#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <cmath>

using namespace std;

// IDSZ   - maximum number of data points in tape 4
const int IDSZ = 2*8000;  //90000;

// IRS    - maximum number of reflections in the pattern
const int IRS = 2*4096; // 20000;

// NATS   - maximum number of atoms in the problem
const int NATS = 2*128; //512;

// MSZ    - maximum number of refinable parameters (matrix size)
const int MSZ = 2*64;  //256;

// NOV    - maximum number of Bragg reflections contributing to one step
const int NOV = 4*512; //15000;



// NPROF Profile selection
const int _Gaussian			= 0;
const int _Lorentzian		= 1;
const int _Mod1				= 2;
const int _Mod2				= 3;
const int _SplitPearsonVII	= 4;
const int _pseudoVoigt		= 5;
const int _PearsonVII		= 6;
const int _TCHZ				= 7;




//     nfinal - dimension of the table FINAL(nfinal) in COMMON/ALLP/
//		It must be equal to (#atoms*11 + #phases*27 + 11), at least,
//		where #atoms & #phases are actually numbers of atoms
//		and phases in the input control file for the current refinement.
// 	Previous declaration FINAL(8*MSZ,2) caused error in case of many atoms.
const int NFINAL = 6048;

const string TABNC[85+1] = {
    "",
    "H","HE","LI","BE","B","C","N","O","F","NE","NA","MG",
    "AL","SI","P","S","CL","AR","K","CA","SC","TI","V","CR","MN",
    "FE","CO","NI","CU","ZN","GA","GE","AS","SE","BR","KR","RB","SR","Y","ZR","NB",
    "MO","TC","RU","RH","PD","AG","CD","IN","SN","SB","TE","I","XE","CS","BA","LA",
    "CE","PR","ND","EU","GD","TB","DY","HO","ER","TM","YB","LU","HF","TA","W",
    "RE","OS","IR","PT","AU","HG","TL","PB","BI","TH","U","NP","PU"
};

const double TABN[85+1] = {
    0.0,
    -0.372,0.3,-0.194,0.774,0.54,0.663,0.94,0.575,0.57,0.46,
    0.351,0.52,0.345,0.415,0.51,0.285,0.958,0.20,0.37,0.47,1.18,
    -0.33,-0.05,0.352,-0.36,0.95,0.25,1.03,0.76,0.57,0.72,0.84,0.64,
    0.80,0.68,0.74,0.70,0.69,0.79,0.69,0.71,0.66,0.68,0.73,0.59,0.60,
    0.61,0.37,0.39,0.61,0.56,0.54,0.52,0.47,0.55,0.52,0.83,0.55,0.44,
    0.72,0.55,1.5,0.76,1.69,0.85,0.79,0.69,1.26,0.73,0.78,0.70,0.48,
    0.92,1.07,1.06,0.95,0.76,1.27,0.89,0.94,0.85,0.99,0.84,1.055,0.75
};

//  Dispersion Coefficients from International Tables
//  Volume C page 219-222 for wavelengths
//    2.748510   2.289620   1.935970   1.788965   1.540520
//     Ti         Cr         Fe         Co         Cu
//    0.709260   0.559360   0.215947   0.209010   0.180195
//      Mo         Ag         Ta         W          Au
//     first  2 lines = 10 fp  values
//     second 2 lines = 10 fpp values
//  From the codes of Ian Madsen , added in DBWS on 11 Feb 98
const double TBD[128+1][20+1] = {
    {0.0000,0.0000,    0.0000,    0.0000,    0.0000,    0.0000, 0.0000,    0.0000,    0.0000,    0.0000,    0.0000 ,
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000, 0.0000,    0.0000,    0.0000,    0.0000,    0.0000},

    //  Dispersion Coefficients for H
    {0.0000,0.0000,    0.0000,    0.0000,    0.0000,    0.0000, 0.0000,    0.0000,    0.0000,    0.0000,    0.0000 ,
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000, 0.0000,    0.0000,    0.0000,    0.0000,    0.0000},

    //  Dispersion Coefficients for He
    {0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,  0.0000,    0.0000,    0.0000,    0.0000,    0.0000,
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,  0.0000,    0.0000,    0.0000,    0.0000,    0.0000 },

    //  Dispersion Coefficients for Li
    {0.0000,    0.0035,    0.0023,    0.0015,    0.0013,    0.0008,   -0.0003,   -0.0004,   -0.0006,   -0.0006,   -0.0006,
    0.0013,    0.0008,    0.0006,    0.0005,    0.0003,     0.0001,    0.0000,    0.0000,    0.0000,    0.0000},

    //  Dispersion Coefficients for Be
    {0.0000,    0.0117,    0.0083,    0.0060,    0.0052,    0.0038,    0.0005,    0.0001,   -0.0005,   -0.0005,   -0.0005,
    0.0050,    0.0033,    0.0023,    0.0019,    0.0014,     0.0002,    0.0001,    0.0000,    0.0000,    0.0000},

    //  Dispersion Coefficients for B
    {0.0000,    0.0263,    0.0190,    0.0140,    0.0121,    0.0090,     0.0013,    0.0004,   -0.0009,   -0.0009,   -0.0010,
    0.0139,    0.0094,    0.0065,    0.0055,    0.0039,     0.0007,    0.0004,    0.0000,    0.0000,    0.0000},

    //  Dispersion Coefficients for C
    {0.0000,    0.0490,    0.0364,    0.0273,    0.0237,    0.0181,     0.0033,    0.0015,   -0.0012,   -0.0013,   -0.0014,
    0.0313,    0.0213,    0.0148,    0.0125,    0.0091,     0.0016,    0.0009,    0.0001,    0.0001,    0.0001},

    //  Dispersion Coefficients for N
    {0.0000,    0.0807,    0.0606,    0.0461,    0.0403,    0.0311,     0.0061,    0.0030,   -0.0020,   -0.0020,   -0.0023,
    0.0606,    0.0416,    0.0293,    0.0248,    0.0180,     0.0033,    0.0019,    0.0002,    0.0002,    0.0001},

    //  Dispersion Coefficients for O
    {0.0000,    0.1213,    0.0928,    0.0716,    0.0630,    0.0492,     0.0106,    0.0056,   -0.0025,   -0.0026,   -0.0030,
    0.1057,    0.0731,    0.0518,    0.0440,    0.0322,     0.0060,    0.0036,    0.0004,    0.0004,    0.0003},

    //  Dispersion Coefficients for F
    {0.0000,    0.1700,    0.1324,    0.1037,    0.0920,    0.0727,     0.0171,    0.0096,   -0.0027,   -0.0028,   -0.0034,
    0.1710,    0.1192,    0.0851,    0.0725,    0.0534,     0.0103,    0.0061,    0.0007,    0.0007,    0.0005},

    //  Dispersion Coefficients for Ne
    {0.0000,    0.2257,    0.1793,    0.1426,    0.1273,    0.1019,     0.0259,    0.0152,   -0.0025,   -0.0028,   -0.0037,
    0.2621,    0.1837,    0.1318,    0.1126,    0.0833,     0.0164,    0.0098,    0.0012,    0.0011,    0.0008},

    //  Dispersion Coefficients for Na
    {0.0000,    0.2801,    0.2295,    0.1857,    0.1670,    0.1353,     0.0362,    0.0218,   -0.0028,   -0.0031,   -0.0044,
    0.3829,    0.2699,    0.1957,    0.1667,    0.1239,     0.0249,    0.0150,    0.0019,    0.0017,    0.0012},

    //  Dispersion Coefficients for Mg
    {0.0000,    0.3299,    0.2778,    0.2309,    0.2094,    0.1719,     0.0486,    0.0298,   -0.0030,   -0.0034,   -0.0052,
    0.5365,    0.3812,    0.2765,    0.2373,    0.1771,     0.0363,    0.0220,    0.0028,    0.0026,    0.0018},

    //  Dispersion Coefficients for Al
    {0.0000,    0.3760,    0.3260,    0.2774,    0.2551,    0.2130,     0.0645,    0.0406,   -0.0020,   -0.0026,   -0.0050,
    0.7287,    0.5212,    0.3807,    0.3276,    0.2455,     0.0514,    0.0313,    0.0040,    0.0037,    0.0027},

    //  Dispersion Coefficients for Si
    {0.0000,    0.3921,    0.3647,    0.3209,    0.2979,    0.2541,     0.0817,    0.0522,   -0.0017,   -0.0025,   -0.0055,
    0.9619,    0.6921,    0.5081,    0.4384,    0.3302,     0.0704,    0.0431,    0.0056,    0.0052,    0.0038},

    //  Dispersion Coefficients for P
    {0.0000,    0.3821,    0.3898,    0.3592,    0.3388,    0.2955,     0.1023,    0.0667,   -0.0002,   -0.0012,   -0.0050,
    1.2423,    0.8984,    0.6628,    0.5731,    0.4335,     0.0942,    0.0580,    0.0077,    0.0071,    0.0052},

    //  Dispersion Coefficients for S
    {0.0000,    0.3167,    0.3899,    0.3848,    0.3706,    0.3331,    0.1246,    0.0826,    0.0015,    0.0003,   -0.0045,
    1.5665,    1.1410,    0.8457,    0.7329,    0.5567,     0.1234,    0.0763,    0.0103,    0.0096,    0.0069},

    //  Dispersion Coefficients for Cl
    {0.0000,    0.1832,    0.3508,    0.3920,    0.3892,    0.3639,     0.1484,    0.0998,    0.0032,    0.0017,   -0.0042,
    1.9384,    1.4222,    1.0596,    0.9202,    0.7018,     0.1585,    0.0984,    0.0134,    0.0125,    0.0091},

    //  Dispersion Coefficients for Ar
    {0.0000,   -0.0656,    0.2609,    0.3696,    0.3880,    0.3843,     0.1743,    0.1191,    0.0059,    0.0041,   -0.0030,
    2.3670,    1.7458,    1.3087,    1.1388,    0.8717,     0.2003,    0.1249,    0.0174,    0.0162,    0.0118},

    //  Dispersion Coefficients for K
    {0.0000,   -0.5083,    0.0914,    0.3068,    0.3532,    0.3868,     0.2009,    0.1399,    0.0089,    0.0067,   -0.0017,
    2.8437,    2.1089,    1.5888,    1.3865,    1.0657,     0.2494,    0.1562,    0.0219,    0.0204,    0.0149},

    //  Dispersion Coefficients for Ca
    {0.0000,   -1.3666,   -0.1987,    0.1867,    0.2782,    0.3641,     0.2262,    0.1611,    0.0122,    0.0097,   -0.0002,
    3.3694,    2.5138,    1.9032,    0.6648,    1.2855,     0.3064,    0.1926,    0.0273,    0.0255,    0.0187},

    //  Dispersion Coefficients for Sc
    {0.0000,   -5.4265,   -0.6935,   -0.0120,    0.1474,    0.3119,     0.2519,    0.1829,    0.0159,    0.0130,    0.0015,
    4.0017,    2.9646,    2.2557,    1.9774,    1.5331,     0.3716,    0.2348,    0.0338,    0.0315,    0.0231},

    //  Dispersion Coefficients for Ti
    {0.0000,   -2.2250,   -1.6394,   -0.3318,   -0.0617,    0.2191,     0.2776,    0.2060,    0.0212,    0.0179,    0.0047,
    0.5264,    3.4538,    2.6425,    2.3213,    1.8069,     0.4457,    0.2830,    0.0414,    0.0387,    0.0284},

    //  Dispersion Coefficients for V
    {0.0000,   -1.6269,   -4.4818,   -0.8645,   -0.3871,    0.0687,     0.3005,    0.2276,    0.0259,    0.0221,    0.0070,
    0.6340,    0.4575,    3.0644,    2.6994,    2.1097,     0.5294,    0.3376,    0.0500,    0.0468,    0.0344},

    //  Dispersion Coefficients for Cr
    {0.0000,   -1.2999,   -2.1308,   -1.9210,   -0.9524,   -0.1635,     0.3209,    0.2496,    0.0314,    0.0272,    0.0101,
    0.7569,    0.5468,    3.5251,    3.1130,    2.4439,     0.6236,    0.3992,    0.0599,    0.0561,    0.0413},

    //  Dispersion Coefficients for Mn
    {0.0000,   -1.0732,   -1.5980,   -3.5716,   -2.0793,   -0.5299,     0.3368,    0.2704,    0.0377,    0.0330,    0.0139,
    0.8956,    0.6479,    0.4798,    3.5546,    2.8052,     0.7283,    0.4681,    0.0712,    0.0666,    0.0492},

    //  Dispersion Coefficients for Fe
    {0.0000,   -0.8901,   -1.2935,   -2.0554,   -3.3307,   -1.1336,     0.3463,    0.2886,    0.0438,    0.0386,    0.0173,
    1.0521,    0.7620,    0.5649,    0.4901,    3.1974,     0.8444,    0.5448,    0.0840,    0.0787,    0.0582},

    //  Dispersion Coefficients for Co
    {0.0000,   -0.7307,   -1.0738,   -1.5743,   -2.0230,   -2.3653,     0.3494,    0.3050,    0.0512,    0.0454,    0.0219,
    1.2272,    0.8897,    0.6602,    0.5731,    3.6143,     0.9721,    0.6296,    0.0984,    0.0921,    0.0682},

    //  Dispersion Coefficients for Ni
    {0.0000,   -0.5921,   -0.9005,   -1.2894,   -1.5664,   -3.0029,     0.3393,    0.3147,    0.0563,    0.0500,    0.0244,
    1.4240,    1.0331,    0.7671,    0.6662,    0.5091,     1.1124,    0.7232,    0.1146,    0.1074,    0.0796},

    //  Dispersion Coefficients for Cu
    {0.0000,   -0.4430,   -0.7338,   -1.0699,   -1.2789,   -1.9646,     0.3201,    0.3240,    0.0647,    0.0579,    0.0298,
    1.6427,    1.1930,    0.8864,    0.7700,    0.5888,     1.2651,    0.8257,    0.1326,    0.1242,    0.0922},

    //  Dispersion Coefficients for Zn
    {0.0000,   -0.3524,   -0.6166,   -0.9134,   -1.0843,   -1.5491,     0.2839,    0.3242,    0.0722,    0.0648,    0.0344,
    1.8861,    1.3712,    1.0193,    0.8857,    0.6778,     1.4301,    0.9375,    0.1526,    0.1430,    0.1063},

    //  Dispersion Coefficients for Ga
    {0.0000,   -0.2524,   -0.4989,   -0.7701,   -0.9200,   -1.2846,     0.2307,    0.3179,    0.0800,    0.0721,    0.0393,
    2.1518,    1.5674,    1.1663,    1.0138,    0.7763,     1.6083,    1.0589,    0.1745,    0.1636,    0.1218},

    //  Dispersion Coefficients for Ge
    {0.0000,   -0.1549,   -0.3858,   -0.6412,   -0.7781,   -1.0885,     0.1547,    0.3016,    0.0880,    0.0796,    0.0445,
    2.4445,    1.7841,    1.3291,    1.1557,    0.8855,     1.8001,    1.1903,    0.1987,    0.1863,    0.1389},

    //  Dispersion Coefficients for As
    {0.0000,   -0.0687,   -0.2871,   -0.5260,   -0.6523,   -0.9300,     0.0499,    0.2758,    0.0962,    0.0873,    0.0501,
    2.7627,    2.0194,    1.5069,    1.3109,    1.0051,     2.0058,    1.3314,    0.2252,    0.2112,    0.1576},

    //  Dispersion Coefficients for Se
    {0.0000,    0.0052,   -0.1919,   -0.4179,   -0.5390,   -0.7943,    -0.0929,    0.2367,    0.1047,    0.0954,    0.0560,
    3.1131,    2.2784,    1.7027,    1.4821,    1.1372,     2.2259,    1.4831,    0.2543,    0.2386,    0.1782},

    //  Dispersion Coefficients for Br
    {0.0000,    0.0592,   -0.1095,   -0.3244,   -0.4363,   -0.6763,    -0.2901,    0.1811,    0.1106,    0.1026,    0.0613,
    3.4901,    2.5578,    1.9140,    1.6673,    1.2805,     2.4595,    1.6452,    0.2858,    0.2682,    0.2006},

    //  Dispersion Coefficients for Kr
    {0.0000,    0.1009,   -0.0316,   -0.2303,   -0.3390,   -0.5657,    -0.5574,    0.1067,    0.1180,    0.1082,    0.0668,
    3.9083,    2.8669,    2.1472,    1.8713,    1.4385,     2.7079,    1.8192,    0.3197,    0.3003,    0.2251},

    //  Dispersion Coefficients for Rb
    {0.0000,    0.1056,    0.0247,   -0.1516,   -0.2535,   -0.4688,    -0.9393,    0.0068,    0.1247,    0.1146,    0.0717,
    4.3505,    3.1954,    2.3960,    2.0893,    1.6079,     2.9676,    2.0025,    0.3561,    0.3346,    0.2514},

    //  Dispersion Coefficients for Sr
    {0.0000,    0.1220,    0.1037,   -0.0489,   -0.1448,   -0.3528,    -1.5307,   -0.1172,    0.1321,    0.1219,    0.0769,
    4.8946,    3.6029,    2.7060,    2.3614,    1.8200,     3.2498,    2.2025,    0.3964,    0.3726,    0.2805},

    //  Dispersion Coefficients for Y
    {0.0000,    0.0654,    0.1263,    0.0138,   -0.0720,   -0.2670,    -2.7962,   -0.2879,    0.1380,    0.1278,    0.0819,
    5.4198,    3.9964,    3.0054,    2.6241,    2.0244,     3.5667,    2.4099,    0.4390,    0.4128,    0.3112},

    //  Dispersion Coefficients for Zr
    {0.0000,   -0.0304,    0.1338,    0.0659,   -0.0066,   -0.1862,    -2.9673,   -0.5364,    0.1431,    0.1329,    0.0863,
    5.9818,    4.4226,    3.3301,    2.9086,    2.2449,     0.5597,    2.6141,    0.4852,    0.4562,    0.3443},

    //  Dispersion Coefficients for Nb
    {0.0000,   -0.1659,    0.1211,    0.1072,    0.0496,   -0.1121,    -2.0727,   -0.8282,    0.1471,    0.1371,    0.0905,
    6.5803,    4.8761,    3.6768,    3.2133,    2.4826,     0.6215,    2.8404,    0.5342,    0.5025,    0.3797},

    //  Dispersion Coefficients for Mo
    {0.0000,   -0.3487,    0.0801,    0.1301,    0.0904,   -0.0483,    -1.6832,   -1.2703,    0.1487,    0.1391,    0.0934,
    7.2047,    5.3484,    4.0388,    3.5326,    2.7339,     0.6857,    3.0978,    0.5862,    0.5517,    0.4177},

    //  Dispersion Coefficients for Tc
    {0.0000,   -0.6073,   -0.0025,    0.1314,    0.1164,    0.0057,    -1.4390,   -2.0087,    0.1496,    0.1406,    0.0960,
    7.8739,    5.8597,    4.4331,    3.8799,    3.0049,     0.7593,    3.3490,    0.6424,    0.6047,    0.4582},

    //  Dispersion Coefficients for Ru
    {0.0000,   -0.9294,   -0.1091,    0.1220,    0.1331,    0.0552,    -1.2594,   -5.3630,    0.1491,    0.1409,    0.0981,
    8.5988,    6.4069,    4.8540,    4.2509,    3.2960,     0.8363,    3.6506,    0.7016,    0.6607,    0.5014},

    //  Dispersion Coefficients for Rh
    {0.0000,   -1.3551,   -0.2630,    0.0861,    0.1305,    0.0927,    -1.1178,   -2.5280,    0.1445,    0.1373,    0.0970,
    9.3504,    6.9820,    5.2985,    4.6432,    3.6045,     0.9187,    0.5964,    0.7639,    0.7195,    0.5469},

    //  Dispersion Coefficients for Pd
    {0.0000,   -1.9086,   -0.4640,    0.0279,    0.1128,    0.1215,    -0.9988,   -1.9556,    0.1387,    0.1327,    0.0959,
    10.1441,    7.5938,    5.7719,    5.0613,    3.9337,     1.0072,    0.6546,    0.8302,    0.7822,    0.5955},

    //  Dispersion Coefficients for Ag
    {0.0000,   -2.5003,   -0.7387,   -0.0700,    0.0634,    0.1306,    -0.8971,   -1.6473,    0.1295,    0.1251,    0.0928,
    10.9916,    8.2358,    6.2709,    5.5027,    4.2820,     1.1015,    0.7167,    0.9001,    0.8484,    0.6469},

    //  Dispersion Coefficients for Cd
    {0.0000,   -3.5070,   -1.1086,   -0.2163,   -0.0214,    0.1185,    -0.8075,   -1.4396,    0.1171,    0.1147,    0.0881,
    11.9019,    8.9174,    6.8017,    5.9728,    4.6533,     1.2024,    0.7832,    0.9741,    0.9185,    0.7013},

    //  Dispersion Coefficients for In
    {0.0000,   -5.1325,   -1.5975,   -0.4165,   -0.1473,    0.0822,    -0.7276,   -1.2843,    0.1013,    0.1012,    0.0816,
    12.6310,    9.6290,    7.3594,    6.4674,    5.0449,     1.3100,    0.8542,    1.0519,    0.9922,    0.7587},

    //  Dispersion Coefficients for Sn
    {0.0000,   -7.5862,   -2.2019,   -0.6686,   -0.3097,    0.0259,    -0.6537,   -1.1587,    0.0809,    0.0839,    0.0728,
    13.5168,   10.3742,    7.9473,    6.9896,    5.4591,     1.4246,    0.9299,    1.1337,    1.0697,    0.8192},

    //  Dispersion Coefficients for Sb
    {0.0000,   -9.2145,   -3.0637,   -0.9868,   -0.5189,   -0.0562,    -0.5866,   -1.0547,    0.0559,    0.0619,    0.0613,
    12.7661,   11.1026,    8.5620,    7.5367,    5.8946,     1.5461,    1.0104,    1.2196,    1.1512,    0.8830},

    //  Dispersion Coefficients for Te
    {0.0000,  -11.6068,   -4.2407,   -1.4022,   -0.7914,   -0.1759,    -0.5308,   -0.9710,    0.0216,    0.0316,    0.0435,
    10.1013,   11.8079,    9.2067,    8.1113,    6.3531,     1.6751,    1.0960,    1.3095,    1.2366,    0.9499},

    //  Dispersion Coefficients for I
    {0.0000,  -13.9940,   -5.6353,   -1.9032,   -1.1275,   -0.3257,    -0.4742,   -0.8919,   -0.0146,   -0.0001,    0.0259,
    3.4071,   12.6156,    9.8852,    8.7159,    6.8362,     1.8119,    1.1868,    1.4037,    1.3259,    1.0201},

    //  Dispersion Coefficients for Xe
    {0.0000,   -9.6593,   -8.1899,   -2.6313,   -1.5532,   -0.5179,    -0.4205,   -0.8200,   -0.0565,   -0.0367,    0.0057,
    3.7063,   11.7407,   10.5776,    9.3585,    7.3500,     1.9578,    1.2838,    1.5023,    1.4195,    1.0938},

    //  Dispersion Coefficients for Cs
    {0.0000,   -8.1342,  -10.3310,   -3.5831,   -2.1433,   -0.7457,    -0.3680,   -0.7527,   -0.1070,   -0.0809,   -0.0194,
    4.0732,   12.8551,   11.2902,   10.0454,    7.9052,     2.1192,    1.3916,    1.6058,    1.5179,    1.1714},

    //  Dispersion Coefficients for Ba
    {0.0000,   -7.2079,  -11.0454,   -4.6472,   -2.7946,   -1.0456,    -0.3244,   -0.6940,   -0.1670,   -0.1335,   -0.0494,
    4.4110,   10.0919,   12.0003,   10.7091,    8.4617,     2.2819,    1.5004,    1.7127,    1.6194,    1.2517},

    //  Dispersion Coefficients for La
    {0.0000,   -6.5722,  -12.8190,   -6.3557,   -3.6566,   -1.4094,    -0.2871,   -0.6411,   -0.2363,   -0.1940,   -0.0835,
    4.7587,    3.5648,   12.8927,   11.4336,    9.0376,     2.4523,    1.6148,    1.8238,    1.7250,    1.3353},

    //  Dispersion Coefficients for Ce
    {0.0000,   -6.0641,   -9.3304,   -8.0962,   -4.8792,   -1.8482,    -0.2486,   -0.5890,   -0.3159,   -0.2633,   -0.1222,
    5.1301,    3.8433,   11.8734,   12.1350,    9.6596,     2.6331,    1.7358,    1.9398,    1.8353,    1.4227},

    //  Dispersion Coefficients for Pr
    {0.0000,   -5.6727,   -7.9841,  -10.9279,   -6.7923,   -2.4164,    -0.2180,   -0.5424,   -0.4096,   -0.3443,   -0.1666,
    5.5091,    4.1304,    9.2394,   12.8653,   10.2820,     2.8214,    1.8624,    2.0599,    1.9496,    1.5136},

    //  Dispersion Coefficients for Nd
    {0.0000,   -5.3510,   -7.1451,  -10.5249,   -8.1618,   -3.1807,    -0.1943,   -0.5012,   -0.5194,   -0.4389,   -0.2183,
    5.9005,    4.4278,    9.9814,   11.9121,   10.9079,     3.0179,    1.9950,    2.1843,    2.0679,    1.6077},

    //  Dispersion Coefficients for Pm
    {0.0000,   -5.0783,   -6.5334,  -13.2062,  -10.0720,   -4.0598,    -0.1753,   -0.4626,   -0.6447,   -0.5499,   -0.2776,
    6.3144,    4.7422,    3.6278,    9.2324,   11.5523,     3.2249,    2.1347,    2.3143,    2.1906,    1.7056},

    //  Dispersion Coefficients for Sm
    {0.0000,   -4.8443,   -6.0570,   -9.3497,  -10.2609,   -5.3236,    -0.1638,   -0.4287,   -0.7989,   -0.6734,   -0.3455,
    6.7524,    5.0744,    3.8839,    9.9412,   12.2178,     3.4418,    2.2815,    2.4510,    2.3197,    1.8069},

    //  Dispersion Coefficients for Eu
    {0.0000,   -4.6288,   -5.6630,   -7.9854,  -13.5405,   -8.9294,    -0.1578,   -0.3977,   -0.9903,   -0.8137,   -0.4235,
    7.2035,    5.4178,    4.1498,    3.6550,   11.1857,     3.6682,    2.4351,    2.5896,    2.4526,    1.9120},

    //  Dispersion Coefficients for Gd
    {0.0000,   -4.5094,   -5.3778,   -7.1681,   -9.3863,   -8.8380,    -0.1653,   -0.3741,   -1.2279,   -1.0234,   -0.5140,
    7.6708,    5.7756,    4.4280,    3.9016,   11.9157,     3.9035,    2.5954,    2.7304,    2.5878,    2.0202},

    //  Dispersion Coefficients for Tb
    {0.0000,   -4.3489,   -5.0951,   -6.5583,   -8.0413,   -9.1472,    -0.1723,   -0.3496,   -1.5334,   -1.2583,   -0.6165,
    8.1882,    6.1667,    4.7292,    4.1674,    9.1891,     4.1537,    2.7654,    2.8797,    2.7310,    2.1330},

    //  Dispersion Coefficients for Dy
    {0.0000,   -4.1616,   -4.8149,   -6.0597,   -7.1503,   -9.8046,    -0.1892,   -0.3302,   -1.9594,   -1.5632,   -0.7322,
    8.6945,    6.5527,    5.0280,    4.4320,    9.8477,     4.4098,    2.9404,    3.0274,    2.8733,    2.2494},

    //  Dispersion Coefficients for Ho
    {0.0000,   -4.0280,   -4.5887,   -5.6628,   -6.5338,  -14.9734,    -0.2175,   -0.3168,   -2.6705,   -1.9886,   -0.8709,
    9.2302,    6.9619,    5.3451,    4.7129,    3.7046,     4.6783,    3.1241,    3.1799,    3.0218,    2.3711},

    //  Dispersion Coefficients for Er
    {0.0000,   -3.9471,   -4.4106,   -5.3448,   -6.0673,   -9.4367,    -0.2586,   -0.3091,   -5.5645,   -2.6932,   -1.0386,
    9.7921,    7.3910,    5.6776,    5.0074,    3.9380,     4.9576,    3.3158,    0.6167,    3.1695,    2.4949},

    //  Dispersion Coefficients for Tm
    {0.0000,   -3.9079,   -4.2698,   -5.0823,   -5.6969,   -8.0393,    -0.3139,   -0.3084,   -2.8957,   -5.6057,   -1.2397,
    10.3763,    7.8385,    6.0249,    5.3151,    4.1821,     5.2483,    3.5155,    0.6569,    0.6192,    2.6240},

    //  Dispersion Coefficients for Yb
    {0.0000,   -3.8890,   -4.1523,   -4.8591,   -5.3940,   -7.2108,    -0.3850,   -0.3157,   -2.4144,   -2.9190,   -1.4909,
    10.9742,    8.2969,    6.3813,    5.6309,    4.4329,     5.5486,    3.7229,    0.6994,    0.6592,    2.7538},

    //  Dispersion Coefficients for Lu
    {0.0000,   -3.9056,   -4.0630,   -4.6707,   -5.1360,   -6.6179,    -0.4720,   -0.3299,   -2.1535,   -2.4402,   -1.8184,
    11.5787,    8.7649,    6.7484,    5.9574,    4.6937,     5.8584,    3.9377,    0.7436,    0.7010,    2.8890},

    //  Dispersion Coefficients for Hf
    {0.0000,   -4.0452,   -4.0564,   -4.4593,   -4.9466,   -6.1794,    -0.5830,   -0.3548,   -1.9785,   -2.1778,   -2.2909,
    12.2546,    9.2832,    7.1518,    6.3150,    4.9776,     6.1852,    4.1643,    0.7905,    0.7454,    3.0246},

    //  Dispersion Coefficients for Ta
    {0.0000,   -4.0905,   -3.9860,   -4.3912,   -4.7389,   -5.7959,    -0.7052,   -0.3831,   -1.8534,   -2.0068,   -3.1639,
    12.9479,    9.8171,    7.5686,    6.6850,    5.2718,     6.5227,    4.3992,    0.8392,    0.7915,    3.1610},

    //  Dispersion Coefficients for W
    {0.0000,   -4.1530,   -3.9270,   -4.2486,   -4.5529,   -5.4734,    -0.8490,   -0.4201,   -1.7565,   -1.8819,   -3.8673,
    13.6643,   10.3696,    8.0005,    7.0688,    5.5774,     6.8722,    4.6430,    0.8905,    0.8388,    0.6433},

    //  Dispersion Coefficients for Re
    {0.0000,   -4.2681,   -3.9052,   -4.1390,   -4.4020,   -5.2083,    -1.0185,   -0.4693,   -1.6799,   -1.7868,   -2.8429,
    14.3931,   10.9346,    8.4435,    7.4631,    5.8923,     7.2310,    4.8944,    0.9441,    0.8907,    0.6827},

    //  Dispersion Coefficients for Os
    {0.0000,   -4.4183,   -3.9016,   -4.0478,   -4.2711,   -4.9801,    -1.2165,   -0.5280,   -1.6170,   -1.7107,   -2.4688,
    15.1553,   11.5251,    8.9067,    7.8753,    6.2216,     7.6030,    5.1558,    1.0001,    0.9437,    0.7238},

    //  Dispersion Coefficients for Ir
    {0.0000,   -4.5860,   -3.9049,   -3.9606,   -4.1463,   -4.7710,    -1.4442,   -0.5977,   -1.5648,   -1.6486,   -2.2499,
    15.9558,   12.1453,    9.3923,    8.3074,    6.5667,     7.9887,    5.4269,    1.0589,    0.9993,    0.7669},

    //  Dispersion Coefficients for Pt
    {0.0000,   -4.8057,   -3.9435,   -3.8977,   -4.0461,   -4.5932,    -1.7033,   -0.6812,   -1.5228,   -1.5998,   -2.1036,
    16.7870,   12.7910,    9.8985,    8.7578,    6.9264,     8.3905,    5.7081,    1.1193,    1.0565,    0.8116},

    //  Dispersion Coefficients for Au
    {0.0000,   -5.0625,   -3.9908,   -3.8356,   -3.9461,   -4.4197,    -2.0133,   -0.7638,   -1.4693,   -1.5404,   -1.9775,
    17.6400,   13.4551,   10.4202,    9.2222,    7.2980,     8.8022,    5.9978,    1.1833,    1.1171,    0.8589},

    //  Dispersion Coefficients for Hg
    {0.0000,   -5.4327,   -4.1029,   -3.8228,   -3.8921,   -4.2923,    -2.3894,   -0.8801,   -1.4389,   -1.5055,   -1.8958,
    18.5241,   14.1473,   10.9650,    9.7076,    7.6849,     9.2266,    6.2989,    1.2483,    1.1796,    0.9080},

    //  Dispersion Coefficients for Tl
    {0.0000,   -5.8163,   -4.2233,   -3.8103,   -3.8340,   -4.1627,    -2.8358,   -1.0117,   -1.4111,   -1.4740,   -1.8288,
    19.4378,   14.8643,   11.5300,   10.2108,    8.0900,     9.6688,    6.6090,    1.3189,    1.2456,    0.9594},

    //  Dispersion Coefficients for Pb
    {0.0000,   -6.4779,   -4.4167,   -3.8519,   -3.8236,   -4.0753,    -3.3944,   -1.1676,   -1.3897,   -1.4497,   -1.7773,
    20.3336,   15.5987,   12.1106,   10.7292,    8.5060,    10.1111,    6.9287,    1.3909,    1.3137,    1.0127},

    //  Dispersion Coefficients for Bi
    {0.0000,   -7.0419,   -4.6533,   -3.9228,   -3.8408,   -4.0111,    -4.1077,   -1.3494,   -1.3721,   -1.4290,   -1.7346,
    21.2196,   16.3448,   12.7017,   11.2575,    8.9310,    10.2566,    7.2566,    1.4661,    1.3851,    1.0685},

    //  Dispersion Coefficients for Po
    {0.0000,   -7.7195,   -4.9604,   -4.0267,   -3.8855,   -3.9670,    -5.1210,   -1.5613,   -1.3584,   -1.4133,   -1.7005,
    22.1974,   17.1410,   13.3329,   11.8209,    9.3834,    11.0496,    7.5986,    1.5443,    1.4592,    1.1266},

    //  Dispersion Coefficients for At
    {0.0000,   -8.5994,   -5.3399,   -4.1781,   -3.9706,   -3.9588,    -7.9122,   -1.8039,   -1.3540,   -1.4066,   -1.6784,
    23.2213,   17.9390,   13.9709,   12.3915,    9.8433,     9.9777,    7.9509,    1.6260,    1.5367,    1.1876},

    //  Dispersion Coefficients for Rn
    {0.0000,  -10.2749,   -5.7275,   -4.3331,   -4.0549,   -3.9487,    -8.0659,   -2.0847,   -1.3475,   -1.3982,   -1.6571,
    24.2613,   18.7720,   14.6313,   12.9815,   10.3181,    10.4580,    8.3112,    1.7103,    1.6167,    1.2504},

    //  Dispersion Coefficients for Fr
    {0.0000,  -10.8938,   -6.2180,   -4.5387,   -4.1818,   -3.9689,    -7.2224,   -2.4129,   -1.3404,   -1.3892,   -1.6367,
    24.3041,   19.6009,   15.3016,   13.5825,   10.8038,     7.7847,    8.6839,    1.7986,    1.7004,    1.3162},

    //  Dispersion Coefficients for Ra
    {0.0000,  -12.3462,   -6.7502,   -4.7764,   -4.3309,   -4.0088,    -6.7704,   -2.8081,   -1.3462,   -1.3931,   -1.6299,
    25.5374,   20.4389,   15.9778,   14.1902,   11.2969,    8.1435,    9.0614,    1.8891,    1.7863,    1.3840},

    //  Dispersion Coefficients for Ac
    {0.0000,  -12.3496,   -7.4161,   -5.0617,   -4.5270,   -4.0794,    -6.8494,   -3.2784,   -1.3473,   -1.3922,   -1.6190,
    25.1363,   21.3053,   16.6687,   14.8096,   11.7994,     8.5178,    9.4502,    1.9845,    1.8770,    1.4553},

    //  Dispersion Coefficients for Th
    {0.0000,  -13.6049,   -8.2118,   -5.3692,   -4.7310,   -4.1491,    -7.2400,   -3.8533,   -1.3524,   -1.3955,   -1.6136,
    26.2511,   22.2248,   17.4018,   15.4642,   12.3296,    8.8979,    9.8403,    2.0819,    1.9695,    1.5284},

    //  Dispersion Coefficients for Pa
    {0.0000,  -14.4639,   -9.4459,   -5.7337,   -4.9639,   -4.2473,   -8.0334,   -4.6067,   -1.3672,   -1.4083,   -1.6170,
    27.4475,   23.1548,   18.1406,   16.1295,   12.8681,     9.2807,   10.2413,    2.1835,    2.0661,    1.6047},

    //  Dispersion Coefficients for U
    {0.0000,  -12.3528,   -9.9362,   -6.1485,   -5.2392,   -4.3638,    -9.6767,   -5.7225,   -1.3792,   -1.4184,   -1.6188,
    30.1725,   23.1239,   18.8728,   16.7952,   13.4090,     9.6646,   10.6428,    2.2876,    1.1650,    1.6831},

    //  Dispersion Coefficients for Np
    {0.0000,  -17.4143,  -11.1080,   -6.6136,   -5.5633,   -4.5053,   -11.4937,   -6.9995,   -1.3941,   -1.4312,   -1.6231,
    31.7405,   24.1168,   19.6379,   17.4837,   13.9666,     4.1493,    9.5876,    2.3958,    2.2679,    1.7648},

    //  Dispersion Coefficients for Pu
    {0.0000,  -18.0862,  -11.4073,   -6.9721,   -5.8130,   -4.6563,    -9.4100,  -13.5905,   -1.4180,   -1.4527,   -1.6351,
    33.8963,   23.2960,   20.1548,   17.9579,   14.3729,     4.3056,    6.9468,    2.4979,    2.3652,    1.8430},

    //  Dispersion Coefficients for Am
    {0.0000,  -19.7042,  -11.7097,   -7.7881,   -6.2920,   -4.8483,    -7.8986,   -6.7022,   -1.4359,   -1.4684,   -1.6424,
    37.3716,   24.5715,   21.1738,   18.8618,   15.0877,     4.5125,    7.3108,    2.6218,    2.4829,    1.9358},

    //  Dispersion Coefficients for Cm
    {0.0000,  -24.9307,  -10.4100,   -8.6102,   -6.7506,   -5.0611,    -7.3248,   -6.2891,   -1.4655,   -1.4952,   -1.6592,
    41.4852,   25.8115,   21.8880,   19.5119,   15.6355,     4.6980,    7.6044,    2.7421,    2.5974,    2.0271},

    //  Dispersion Coefficients for Bk
    {0.0000,  -32.8492,   -9.2185,   -9.3381,   -7.4293,   -5.3481,    -6.8498,   -6.3438,   -1.4932,   -1.5203,   -1.6746,
    32.5421,   29.3028,   21.9514,   20.3581,   16.3190,     4.9086,    7.9477,    2.8653,    2.7147,    2.1208},

    //  Dispersion Coefficients for Cf
    {0.0000,  -23.6520,  -23.5202,   -9.7799,   -7.8616,   -5.5545,    -6.6561,   -6.4144,   -1.5323,   -1.5562,   -1.6984,
    21.9334,   31.2999,   22.4858,   20.8536,   16.7428,     5.0785,    8.1930,    2.9807,    2.8250,    2.2102}
};

// Coefficients for analytical approximation to the scattering factors
// International Tables for Crystallography, Vol C, 501-503 [blue book]

const string TBXC[212+1] = {
    "","H   ","H   ","H-1 ","HE  ","LI  ","LI+1","BE  ","BE+2","B   ","C   ","CVAL",
    "N   ","O   ","O-1 ","F   ","F-1 ","NE  ","NA  ",
    "NA+1","MG  ","MG+2","AL  ","AL+3","SI  ","SIV ",
    "SI+4","P   ","S   ","CL  ","CL-1","AR  ","K   ",
    "K+1 ","CA  ","CA+2","SC  ","SC+3","TI  ","TI+2",
    "TI+3","TI+4","V   ","V+2 ","V+3 ","V+5 ","CR  ",
    "CR+2","CR+3","MN  ","MN+2","MN+3","MN+4","FE  ",
    "FE+2","FE+3","CO  ","CO+2","CO+3","NI  ","NI+2",
    "NI+3","CU  ","CU+1","CU+2","ZN  ","ZN+2","GA  ",
    "GA+3","GE  ","GE+4","AS  ","SE  ","BR  ","BR-1",
    "KR  ","RB  ","RB+1","SR  ","SR+2","Y   ","Y+3 ",
    "ZR  ","ZR+4","NB  ","NB+3","NB+5","MO  ","MO+3",
    "MO+5","MO+6","TC  ","RU  ","RU+3","RU+4","RH  ",
    "RH+3","RH+4","PD  ","PD+2","PD+4","AG  ","AG+1",
    "AG+2","CD  ","CD+2","IN  ","IN+3","SN  ","SN+2",
    "SN+4","SB  ","SB+3","SB+5","TE  ","I   ","I-1 ",
    "XE  ","CS  ","CS+1","BA  ","BA+2","LA  ","LA+3",
    "CE  ","CE+3","CE+4","PR  ","PR+3","PR+4","ND  ",
    "ND+3","PM  ","PM+3","SM  ","SM+3","EU  ","EU+2",
    "EU+3","GD  ","GD+3","TB  ","TB+3","DY  ","DY+3",
    "HO  ","HO+3","ER  ","ER+3","TM  ","TM+3","YB  ",
    "YB+2","YB+3","LU  ","LU+3","HF  ","HF+4","TA  ",
    "TA+5","W   ","W+6 ","RE  ","OS  ","OS+4","IR  ",
    "IR+3","IR+4","PT  ","PT+2","PT+4","AU  ","AU+1",
    "AU+3","HG  ","HG+1","HG+2","TL  ","TL+1","TL+3",
    "PB  ","PB+2","PB+4","BI  ","BI+3","BI+5","PO  ",
    "AT  ","RN  ","FR  ","RA  ","RA+2","AC  ","AC+3",
    "TH  ","TH+4","PA  ","U   ","U+3 ","U+4 ","U+6 ",
    "NP  ","NP+3","NP+4","NP+6","PU  ","PU+3","PU+4",
    "PU+6","AM  ","CM  ","BK  ","CF  "
};

const double TBX[212+1][10+1] = {
    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
    {0.0,    0.493002   ,10.5109   ,0.322912   ,26.1257 ,  0.140191   ,3.14236   ,0.040810   ,57.7997   ,0.003038 , 1.0},
    {0.0,    0.489918   ,20.6593   ,0.262003   ,7.74039   ,0.196767   ,49.5519   ,0.049879   ,2.20159   ,0.001305   ,1.0},
    {0.0,    0.897661   ,53.1368   ,0.565616   ,15.1870   ,0.415815   ,186.576   ,0.116973   ,3.56709   ,0.002389   ,1.0},
    {0.0,    0.8734   , 9.1037   ,  0.6309   , 3.3568   ,0.3112   ,22.9276   ,  0.1780   ,  0.9821   ,  0.0064   ,2.0},
    {0.0,    1.1282   , 3.9546   ,  0.7508   , 1.0524   ,0.6175   ,85.3905   ,  0.4653   ,168.261   ,  0.0377   ,3.0},
    {0.0,    0.6968   , 4.6237   ,  0.7888   , 1.9557   ,0.3414   ,  0.6316   ,  0.1563   ,10.0953   ,  0.0167   ,3.0},
    {0.0,    1.5919   ,43.6427   , 1.1278   , 1.8623   ,   0.5391   ,103.483   ,  0.7029   ,  0.5420   ,  0.0385   ,4.0},
    {0.0,    6.2603   ,  0.0027   ,  0.8849   ,  0.8313   ,0.7993   , 2.2758   ,  0.1647   , 5.1146   ,-6.1092   ,4.0},
    {0.0,    2.0545   ,23.2185   , 1.3326   , 1.0210   ,1.0979   ,60.3498   ,  0.7068   ,  0.1403   , -0.1932   ,5.0},
    {0.0,    2.3100   ,20.8439   , 1.0200   ,10.2075   ,1.5886   , 0.5687   , 0.8650   ,51.6512   , 0.2156   ,6.0},
    {0.0,    2.26069   ,22.6907   ,1.56165   ,0.656665   , 1.05075   ,9.75618   ,0.839259   ,55.5949   ,0.286977  ,6.0},
    {0.0,    12.2126   ,  0.0057   , 3.1322   , 9.8933   ,  2.0125   ,28.9975   , 1.1663   , 0.5826   ,-11.529  ,7.0},
    {0.0,    3.0485   ,13.2771   , 2.2868   , 5.7011   ,  1.5463   ,  0.3239   ,  0.8670   ,32.9089   ,  0.2508 ,  8.0},
    {0.0,    4.19160   ,12.8573   ,1.63969   ,4.17236  ,  1.52673   ,47.0179   ,-20.307   ,-0.01404   ,21.9412 , 8.0},
    {0.0,    3.5392   ,10.2825   , 2.6412   , 4.2944   ,  1.5170   , 0.2615   , 1.0243   ,26.1476   , 0.2776  , 9.0},
    {0.0,    3.63220   ,5.27756   ,3.51057   ,14.7353  ,  1.26064   ,0.442258   ,0.940706   ,47.3437   ,0.653396  , 9.0},
    {0.0,    3.9553   , 8.4042   , 3.1125   , 3.4262   ,  1.4546   ,  0.2306   , 1.1251   ,21.7184   ,  0.3515 , 10.0},
    {0.0,    4.7626   , 3.2850   , 3.1736   , 8.8422   ,  1.2674   ,  0.3136   , 1.1128   ,129.424   ,  0.6760 ,  11.0},
    {0.0,    3.2565   , 2.6671   , 3.9362   , 6.1153   ,  1.3998   ,  0.2001   , 1.0032   ,14.0390   ,  0.4040 ,  11.0},
    {0.0,    5.4204   , 2.8275   , 2.1735   ,79.2611   ,  1.2269   ,  0.3808   , 2.3073   , 7.1937   ,  0.8584 ,  0.12},
    {0.0,    3.4988   , 2.1676   , 3.8378   , 4.7542   ,  1.3284   ,  0.1850   ,  0.8497   ,10.1411   ,  0.4853 ,  0.12},
    {0.0,    6.4202   , 3.0387   , 1.9002   , 0.7426   ,  1.5936   ,31.5472   , 1.9646   ,85.0886   , 1.1151  ,0.13},
    {0.0,    4.17448   ,1.93816   ,3.38760   ,4.14553  , 1.20296   ,0.228753   ,0.528137   ,8.28524   ,0.706786   ,0.13},
    {0.0,    6.2915   , 2.4386   , 3.0353   ,32.3337   ,  1.9891   , 0.6785   , 1.5410   ,81.6937   , 1.1407   ,0.14},
    {0.0,    5.66269   ,2.66520   ,3.07164   ,38.6634   , 2.62446   ,0.916946   ,1.39320   ,93.5458   ,1.24707   ,14.0},
    {0.0,    4.43918   ,1.64167   ,3.20345   ,3.43757   , 1.19453   ,0.214900   ,0.416530   ,6.65365   ,0.746297   ,14.0},
    {0.0,    6.4345   , 1.9067   , 4.1791   ,27.1570   ,  1.7800   , 0.5260   , 1.4908   ,68.1645   , 1.1149   ,15.0},
    {0.0,    6.9053   , 1.4679   , 5.2034   ,22.2151   ,  1.4379   , 0.2536   , 1.5863   ,56.1720   , 0.8669   ,16.0},
    {0.0,    11.4604   , 0.0104   , 7.1964   , 1.1662   ,  6.2556   ,18.5194   , 1.6455   ,47.7784   ,-9.5574   ,17.0},
    {0.0,    18.2915   , 0.0066   , 7.2084   , 1.1717   ,  6.5337   ,19.5424   , 2.3386   ,60.4486   ,-16.378   ,17.0},
    {0.0,    7.4845   , 0.9072   , 6.7723   ,14.8407   ,  0.6539   ,43.8983   , 1.6442   ,33.3929   , 1.4445   ,18.0},
    {0.0,    8.2186   ,12.7949   , 7.4398   , 0.7748   ,  1.0519   ,213.187   , 0.8659   ,41.6841   , 1.4228   ,19.0},
    {0.0,    7.9578   ,12.6331   , 7.4917   , 0.7674   ,  6.3590   , -0.0020   , 1.1915   ,31.9128   ,-4.9978   ,19.0},
    {0.0,    8.6266   ,10.4421   , 7.3873   , 0.6599  ,  1.5899   ,85.7484   , 1.0211   ,178.437   , 1.3751   ,20.0},
    {0.0,    15.6348   , -0.0074   , 7.9518   , 0.6089   ,  8.4372   ,10.3116   , 0.8537   ,25.9905   ,-14.875   ,20.0},
    {0.0,    9.1890   , 9.0213   , 7.3679   , 0.5729   ,  1.6409   ,136.108   , 1.4680   ,51.3531   , 1.3329   ,21.0},
    {0.0,    13.4008   ,0.298540   ,8.02730   ,7.96290   , 1.65943   ,-0.28604   ,1.57936   ,16.0662   ,-6.6667   ,21.0},
    {0.0,    9.7595   , 7.8508   , 7.3558   , 0.5000   ,  1.6991   ,35.6338   , 1.9021   ,116.105   , 1.2807   ,22.0},
    {0.0,    9.11423   ,7.52430   ,7.62174   ,0.457585   , 2.27930   ,19.5361   ,0.087899   ,61.6558   ,0.897155   ,22.0},
    {0.0,    17.7344   ,0.220610   ,8.73816   ,7.04716   , 5.25691   ,-0.15762   ,1.92134   ,15.9768   ,-14.652   ,22.0},
    {0.0,    19.5114   ,0.178847   ,8.23473   ,6.67018  , 2.01341   ,-0.29263   ,1.52080   ,12.9464   ,-13.280  ,22.0},
    {0.0,    10.2971   , 6.8657   , 7.3511   , 0.4385  ,  2.0703   ,26.8938   , 2.0571   ,102.478   , 1.2199   ,23.0},
    {0.0,    10.1060   , 6.8818   , 7.3541   , 0.4409   ,  2.2884   ,20.3004   , 0.0223   ,115.122   , 1.2298   ,23.0},
    {0.0,    9.43141   ,6.39535   ,7.74190   ,0.383349  , 2.15343   ,15.1908   ,0.016865   ,63.9690   ,0.656565   ,23.0},
    {0.0,    15.6887   ,0.679003   ,8.14208   ,5.40135   , 2.03081   ,9.97278   ,-9.5760   ,0.940464   ,1.71430   ,23.0},
    {0.0,    10.6406   , 6.1038   , 7.3537   , 0.3920   ,  3.3240   ,20.2626   , 1.4922   ,98.7399   , 1.1832   ,24.0},
    {0.0,    9.54034   ,5.66078   ,7.75090   ,0.344261   , 3.58274   ,13.3075   ,0.509107   ,32.4224   ,0.616898   ,24.0},
    {0.0,    9.68090   ,5.59463   ,7.81136   ,0.334393   , 2.87603   ,12.8288   ,0.113575   ,32.8761   ,0.518275  ,24.0},
    {0.0,    11.2819   , 5.3409   , 7.3573   , 0.3432   ,  3.0193   ,17.8674   , 2.2441   ,83.7543   , 1.0896   ,25.0},
    {0.0,    10.8061   , 5.2796   , 7.3620   , 0.3435   ,  3.5268   ,14.3430   , 0.2184   ,41.3235   , 1.0874  ,25.0},
    {0.0,    9.84521   ,4.91797   ,7.87194   ,0.294393   , 3.56531   ,10.8171   ,0.323613   ,24.1281   ,0.393974   ,25.0},
    {0.0,    9.96253   ,4.84850   ,7.97057   ,0.283303   , 2.76067   ,10.4852   ,0.054447   ,27.5730   ,0.251877  ,25.0},
    {0.0,    11.7695   , 4.7611   , 7.3573   , 0.3072   ,  3.5222   ,15.3535   , 2.3045   ,76.8805   , 1.0369  ,26.0},
    {0.0,    11.0424   , 4.6538   , 7.3740   , 0.3053   ,  4.1346   ,12.0546   , 0.4399   ,31.2809   , 1.0097   ,26.0},
    {0.0,    11.1764   , 4.6147   , 7.3863   , 0.3005   ,  3.3948   ,11.6729   , 0.0724   ,38.5566   , 0.9707  ,26.0},
    {0.0,    12.2841   , 4.2791   , 7.3409   , 0.2784   ,  4.0034   ,13.5359   , 2.3488   ,71.1692   , 1.0118   ,27.0},
    {0.0,    11.2296   , 4.1231   , 7.3883   , 0.2726   ,  4.7393   ,10.2443   , 0.7108   ,25.6466   , 0.9324   ,27.0},
    {0.0,    10.3380   ,3.90969   ,7.88173   ,0.238668   , 4.76795   ,8.35583   ,0.725591   ,18.3491   ,0.286667  ,27.0},
    {0.0,    12.8376   , 3.8785   , 7.2920   , 0.2565   ,  4.4438   ,12.1763   , 2.3800   ,66.3421   , 1.0341  ,28.0},
    {0.0,    11.4166   , 3.6766   , 7.4005   , 0.2449   ,  5.3442   , 8.8730   , 0.9773   ,22.1626   , 0.8614   ,28.0},
    {0.0,    10.7806   ,3.54770   ,7.75868   ,0.223140   , 5.22746   ,7.64468   ,0.847114   ,16.9673   ,0.386044   ,28.0},
    {0.0,    13.3380   , 3.5828   , 7.1676   , 0.2470   ,  5.6158   ,11.3966   , 1.6735   ,64.8126   , 1.1910  ,29.0},
    {0.0,    11.9475   , 3.3669   , 7.3573   , 0.2274   ,  6.2455   , 8.6625   , 1.5578   ,25.8487   , 0.8900   ,29.0},
    {0.0,    11.8168   ,3.37484   ,7.11181   ,0.244078   , 5.78135   ,7.98760   ,1.14523   ,19.8970   ,1.14431  ,29.0},
    {0.0,    14.0743   , 3.2655   , 7.0318   , 0.2333   ,  5.1652   ,10.3163   , 2.4100   ,58.7097   , 1.3041   ,30.0},
    {0.0,    11.9719   , 2.9946   , 7.3862   , 0.2031  ,  6.4668   , 7.0826   , 1.3940   ,18.0995   , 0.7807   ,30.0},
    {0.0,    15.2354   , 3.0669   , 6.7006   , 0.2412  ,   4.3591   ,10.7805   , 2.9623   ,61.4135   , 1.7189   ,31.0},
    {0.0,    12.6920   ,2.81262   ,6.69883   ,0.227890   , 6.06692   ,6.36441   ,1.00660   ,14.4122   ,1.53545   ,31.0},
    {0.0,    16.0816   , 2.8509   , 6.3747   , 0.2516   ,  3.7068   ,11.4468   , 3.6830   ,54.7625   , 2.1313   ,32.0},
    {0.0,    12.9172   ,2.53718   ,6.70003   ,0.205855   , 6.06791   ,5.47913   ,0.859041   ,11.6030   ,1.45572  ,32.0},
    {0.0,    16.6723   , 2.6345   , 6.0701   , 0.2647   ,  3.4313   ,12.9479   , 4.2779   ,47.7972   , 2.5310  ,33.0},
    {0.0,    17.0006   , 2.4098   , 5.8196   , 0.2726   ,  3.9731   ,15.2372   , 4.3543   ,43.8163   , 2.8409   ,34.0},
    {0.0,    17.1789   , 2.1723   , 5.2358   ,16.5796   ,  5.6377   , 0.2609   , 3.9851   ,41.4328   , 2.9557  ,35.0},
    {0.0,    17.1718   , 2.2059   , 6.3338   ,19.3345   ,  5.5754   , 0.2871   , 3.7272   ,58.1535   , 3.1776   ,35.0},
    {0.0,    17.3555   , 1.9384   , 6.7286   ,16.5623   ,  5.5493   , 0.2261   , 3.5375   ,39.3972   , 2.8250  ,36.0},
    {0.0,    17.1784   , 1.7888   , 9.6435   ,17.3151  ,  5.1399   , 0.2748   , 1.5292   ,164.934   , 3.4873  ,37.0},
    {0.0,    17.5816   , 1.7139   , 7.6598   ,14.7957  ,  5.8981   , 0.1603   , 2.7817   ,31.2087   , 2.0782  ,37.0},
    {0.0,    17.5663   , 1.5564   , 9.8184   ,14.0988   ,  5.4220   , 0.1664   , 2.6694   ,132.376   , 2.5064   ,38.0},
    {0.0,    18.0874   , 1.4907   , 8.1373   ,12.6963  ,  2.5654   ,24.5651   ,-34.193   , -0.0138   ,41.4025  ,38.0},
    {0.0,    17.7760   ,1.40290   ,10.2946   ,12.8006  , 5.72629   ,0.125599   ,3.26588   ,104.354   ,1.91213  ,39.0},
    {0.0,    17.9268   ,1.35417   ,9.15310   ,11.2145   , 1.76795   ,22.6599   ,-33.108   ,-0.01319   ,40.2602  ,39.0},
    {0.0,    17.8765   ,1.27618   ,10.9480   ,11.9160   , 5.41732   ,0.117622   ,3.65721   ,87.6627   ,2.06929   ,40.0},
    {0.0,    18.1668   ,1.21480   ,10.0562   ,10.1483  , 1.01118   ,21.6054   ,-2.6479   ,-0.10276   ,9.41454   ,40.0},
    {0.0,    17.6142   ,1.18865   ,12.0144   ,11.7660   , 4.04183   ,0.204785   ,3.53346   ,69.7957   ,3.75591   ,41.0},
    {0.0,    19.8812   ,0.019175   ,18.0653   ,1.13305   , 11.0177   ,10.1621   ,1.94715   ,28.3389   ,-12.912   ,41.0},
    {0.0,    17.9163   ,1.12446   ,13.3417   ,0.028781   , 10.7990   ,9.28206   ,0.337905   ,25.7228   ,-6.3934   ,41.0},
    {0.0,    3.7025   , 0.2772   ,17.2356   , 1.0958   , 12.8876   ,11.0040   , 3.7429   ,61.6584   , 4.3875   ,42.0},
    {0.0,    21.1664   ,0.014734   ,18.2017   ,1.03031   , 11.7423   ,9.53659   ,2.30951   ,26.6307   ,-14.421  ,42.0},
    {0.0,    21.0149   ,0.014345   ,18.0992   ,1.02238   , 11.4632   ,8.78809   ,0.740625   ,23.3452   ,-14.316   ,42.0},
    {0.0,    17.8871   ,1.03649   ,11.1750   ,8.48061  , 6.57891   ,0.058881   ,0.0000   ,0.0000    ,0.344941  ,42.0},
    {0.0,    19.1301   ,0.864132   ,11.0948   ,8.14487   , 4.64901   ,21.5707   ,2.71263   ,86.8472   ,5.40428  ,43.0},
    {0.0,    19.2674   ,0.808520   ,12.9182   ,8.43467   , 4.86337   ,24.7997   ,1.56756   ,94.2928   ,5.37874   ,44.0},
    {0.0,    18.5638   ,0.847329   ,13.2885   ,8.37164   , 9.32602   ,0.017662   ,3.00964   ,22.8870   ,-3.1892 ,44.0},
    {0.0,    18.5003   ,0.844582   ,13.1787   ,8.12534   , 4.71304   ,0.036495   ,2.18535   ,20.8504   ,1.42357   ,44.0},
    {0.0,    19.2957   ,0.751536   ,14.3501   ,8.21758  , 4.73425   ,25.8749   ,1.28918   ,98.6062   ,5.32800   ,45.0},
    {0.0,    18.8785   ,0.764252   ,14.1259   ,7.84438   , 3.32515   ,21.2487   ,-6.1989   ,-0.01036   ,11.8678   ,45.0},
    {0.0,    18.8545   ,0.760825   ,13.9806   ,7.62436   , 2.53464   ,19.3317   ,-5.6526   ,-0.01020   ,11.2835   ,45.0},
    {0.0,    19.3319   ,0.698655   ,15.5017   ,7.98929  , 5.29537   ,25.2052   ,0.605844   ,76.8986   ,5.26593   ,46.0},
    {0.0,    19.1701   ,0.696219   ,15.2096   ,7.55573   , 4.32234   ,22.5057   ,0.0000   ,0.0000   ,5.29160   ,46.0},
    {0.0,    19.2493   ,0.683839   ,14.7900   ,7.14833   , 2.89289   ,17.9144   ,-7.9492   ,0.005127   ,13.0174   ,46.0},
    {0.0,    19.2808   , 0.6446   ,16.6885   , 7.4726   ,  4.8045   ,24.6605   , 1.0463   ,99.8156   , 5.1790   ,47.0},
    {0.0,    19.1812   ,0.646179   ,15.9719   ,7.19123   , 5.27475   ,21.7326   ,0.357534   ,66.1147   ,5.21572   ,47.0},
    {0.0,    19.1643   ,0.645643   ,16.2456   ,7.18544   , 4.37090   ,21.4072   ,0.0000   ,0.0000   ,5.21404   ,47.0},
    {0.0,    19.2214   , 0.5946   ,17.6444   , 6.9089   ,  4.4610   ,24.7008   , 1.6029   ,87.4825   , 5.0694   ,48.0},
    {0.0,    19.1514   ,0.597922   ,17.2535   ,6.80639   , 4.47128   ,20.2521   ,0.0000   ,0.0000   ,5.11937   ,48.0},
    {0.0,    19.1624   , 0.5476   ,18.5596   , 6.3776   ,  4.2948   ,25.8499   , 2.0396   ,92.8029   , 4.9391  ,49.0},
    {0.0,    19.1045   ,0.551522   ,18.1108   ,6.32470  , 3.78897   ,17.3595   ,0.0000   ,0.0000   ,4.99635  ,49.0},
    {0.0,    19.1889   , 5.8303   ,19.1005   , 0.5031   ,  4.4585   ,26.8909   , 2.4663   ,83.9571   , 4.7821  ,50.0},
    {0.0,    19.1094   , 0.5036   ,19.0548   , 5.8378   ,  4.5648   ,23.3752   , 0.4870   ,62.2061   , 4.7861   ,50.0},
    {0.0,    18.9333   , 5.7640   ,19.7131   , 0.4655   ,  3.4182   ,14.0049   , 0.0193   , -0.7583   , 3.9182   ,50.0},
    {0.0,    19.6418   , 5.3034   ,19.0455   , 0.4607   ,  5.0371   ,27.9074   , 2.6827   ,75.2825   , 4.5909   ,51.0},
    {0.0,    18.9755   ,0.467196   ,18.9330   ,5.22126   , 5.10789   ,19.5902   ,0.288753   ,55.5113   ,4.69626   ,51.0},
    {0.0,    19.8685   ,5.44853   ,19.0302   ,0.467973   , 2.41253   ,14.1259   ,0.0000   ,0.0000   ,4.69263   ,51.0},
    {0.0,    19.9644   ,4.81742   ,19.0138   ,0.420885   , 6.14487   ,28.5284   ,2.52390   ,70.8403   ,4.35200   ,52.0},
    {0.0,    20.1472   , 4.3470   ,18.9949   , 0.3814   ,  7.5138   ,27.7660   , 2.2735   ,66.8776   , 4.0712   ,53.0},
    {0.0,    20.2332   , 4.3579   ,18.9970   , 0.3815   ,  7.8069   ,29.5259   , 2.8868   ,84.9304   , 4.0714   ,53.0},
    {0.0,    20.2933   , 3.9282   ,19.0298   , 0.3440   ,  8.9767   ,26.4659   , 1.9900   ,64.2658   , 3.7118   ,54.0},
    {0.0,    20.3892   , 3.5690   ,19.1062   , 0.3107   , 10.6620   ,24.3879   , 1.4953   ,213.904   , 3.3352   ,55.0},
    {0.0,    20.3524   , 3.5520   ,19.1278   , 0.3086   , 10.2821   ,23.7128   , 0.9615   ,59.4565   , 3.2791   ,55.0},
    {0.0,    20.3361   , 3.2160   ,19.2970   , 0.2756   , 10.8880   ,20.2073   , 2.6959   ,167.202   , 2.7731   ,56.0},
    {0.0,    20.1807   ,3.21367   ,19.1136   ,0.283310   , 10.9054   ,20.0558   ,0.773634   ,51.7460   ,3.02902   ,56.0},
    {0.0,    20.5780   ,2.94817   ,19.5990   ,0.244475   , 11.3727   ,18.7726   ,3.28719   ,133.124   ,2.14678   ,57.0},
    {0.0,    20.2489   ,2.92070   ,19.3763   ,0.250698   , 11.6323   ,17.8211   ,0.336048   ,54.9453   ,2.40860   ,57.0},
    {0.0,    21.1671   ,2.81219   ,19.7695   ,0.226836   , 11.8513   ,17.6083   ,3.33049   ,127.113   ,1.86264   ,58.0},
    {0.0,    20.8036   ,2.77691   ,19.5590   ,0.231540   , 11.9369   ,16.5408   ,0.612376   ,43.1692   ,2.09013   ,58.0},
    {0.0,    20.3235   ,2.65941   ,19.8186   ,0.218850   , 12.1233   ,15.7992   ,0.144583   ,62.2355   ,1.59180   ,58.0},
    {0.0,    22.0440   ,2.77393   ,19.6697   ,0.222087   , 12.3856   ,16.7669   ,2.82428   ,143.644   ,2.05830   ,59.0},
    {0.0,    21.3727   ,2.64520   ,19.7491   ,0.214299   , 12.1329   ,15.3230   ,0.975180   ,36.4065   ,1.77132   ,59.0},
    {0.0,    20.9413   ,2.54467   ,20.0539   ,0.202481   , 12.4668   ,14.8137   ,0.296689   ,45.4643   ,1.24285   ,59.0},
    {0.0,    22.6845   ,2.66248   ,19.6847   ,0.210628   , 12.7740   ,15.8850   ,2.85137   ,137.903   ,1.98486   ,60.0},
    {0.0,    21.9610   ,2.52722   ,19.9339   ,0.199237   , 12.1200   ,14.1783   ,1.51031   ,30.8717   ,1.47588   ,60.0},
    {0.0,    23.3405   ,2.56270   ,19.6095   ,0.202088   , 13.1235   ,15.1009   ,2.87516   ,132.721   ,2.02876   ,61.0},
    {0.0,    22.5527   ,2.41740   ,20.1108   ,0.185769   , 12.0671   ,13.1275   ,2.07492   ,27.4491   ,1.19499   ,61.0},
    {0.0,    24.0042   ,2.47274   ,19.4258   ,0.196451   , 13.4396   ,14.3996   ,2.89604   ,128.007   ,2.20963   ,62.0},
    {0.0,    23.1504   ,2.31641   ,20.2599   ,0.174081   , 11.9202   ,12.1571   ,2.71488   ,24.8242   ,0.954586   ,62.0},
    {0.0,    24.6274   , 2.3879   ,19.0886   , 0.1942   , 13.7603   ,13.7546   , 2.9227   ,123.174   , 2.5745   ,63.0},
    {0.0,    24.0063   ,2.27783   ,19.9504   ,0.173530   , 11.8034   ,11.6096   ,3.87243   ,26.5156   ,1.36389   ,63.0},
    {0.0,    23.7497   ,2.22258   ,20.3745   ,0.163940   , 11.8509   ,11.3110   ,3.26503   ,22.9966   ,0.759344   ,63.0},
    {0.0,    25.0709   ,2.25341   ,19.0798   ,0.181951   , 13.8518   ,12.9331   ,3.54545   ,101.398   ,2.41960   ,64.0},
    {0.0,    24.3466   ,2.13553   ,20.4208   ,0.155525   , 11.8708   ,10.5782   ,3.71490   ,21.7029   ,0.645089   ,64.0},
    {0.0,    25.8976   ,2.24256   ,18.2185   ,0.196143   , 14.3167   ,12.6648   ,2.95354   ,115.362   ,3.58324   ,65.0},
    {0.0,    24.9559   ,2.05601   ,20.3271   ,0.149525   , 12.2471   ,10.0499   ,3.77300   ,21.2773   ,0.691967   ,65.0},
    {0.0,    26.5070   ,2.18020   ,17.6383   ,0.202172   , 14.5596   ,12.1899   ,2.96577   ,111.874   ,4.29728   ,66.0},
    {0.0,    25.5395   ,1.98040   ,20.2861   ,0.143384   , 11.9812   ,9.34972   ,4.50073   ,19.5810   ,0.689690   ,66.0},
    {0.0,    26.9049   ,2.07051   ,17.2940   ,0.197940   , 14.5583   ,11.4407   ,3.63837   ,92.6566   ,4.56796   ,67.0},
    {0.0,    26.1296   ,1.91072   ,20.0994   ,0.139358   , 11.9788   ,8.80018   ,4.93676   ,18.5908   ,0.852795   ,67.0},
    {0.0,    27.6563   ,2.07356   ,16.4285   ,0.223545   , 14.9779   ,11.3604   ,2.98233   ,105.703   ,5.92046   ,68.0},
    {0.0,    26.7220   ,1.84659   ,19.7748   ,0.137290   , 12.1506   ,8.36225   ,5.17379   ,17.8974   ,1.17613   ,68.0},
    {0.0,    28.1819   ,2.02859   ,15.8851   ,0.238849   , 15.1542   ,10.9975   ,2.98706   ,102.961   ,6.75621   ,69.0},
    {0.0,    27.3083   ,1.78711   ,19.3320   ,0.136974   , 12.3339   ,7.96778   ,5.38348   ,17.2922   ,1.63929   ,69.0},
    {0.0,    28.6641   ,1.98890   ,15.4345   ,0.257119   , 15.3087   ,10.6647   ,2.98963   ,100.417   ,7.56672   ,70.0},
    {0.0,    28.1209   ,1.78503   ,17.6817   ,0.159970   , 13.3335   ,8.18304   ,5.14657   ,20.3900   ,3.70983   ,70.0},
    {0.0,    27.8917   ,1.73272   ,18.7614   ,0.138790   , 12.6072   ,7.64412   ,5.47647   ,16.8153   ,2.26001   ,70.0},
    {0.0,    28.9476   ,1.90182   ,15.2208   ,9.98519   , 15.1000   ,0.261033   ,3.71601   ,84.3298   ,7.97628   ,71.0},
    {0.0,    28.4628   ,1.68216   ,18.1210   ,0.142292   , 12.8429   ,7.33727   ,5.59415   ,16.3535   ,2.97573   ,71.0},
    {0.0,    29.1440   ,1.83262   ,15.1726   ,9.59990   , 14.7586   ,0.275116   ,4.30013   ,72.0290   ,8.58154   ,72.0},
    {0.0,    28.8131   ,1.59136   ,18.4601   ,0.128903   , 12.7285   ,6.76232   ,5.59927   ,14.0366   ,2.39699   ,72.0},
    {0.0,    29.2024   ,1.77333   ,15.2293   ,9.37046   , 14.5135   ,0.295977   ,4.76492   ,63.3644   ,9.24354   ,73.0},
    {0.0,    29.1587   ,1.50711   ,18.8407   ,0.116741   , 12.8268   ,6.31524   ,5.38695   ,12.4244   ,1.78555  ,73.0},
    {0.0,    29.0818   ,1.72029   ,15.4300   ,9.22590   , 14.4327   ,0.321703   ,5.11982   ,57.0560   ,9.88750   ,74.0},
    {0.0,    29.4936   ,1.42755   ,19.3763   ,0.104621   , 13.0544   ,5.93667   ,5.06412   ,11.1972   ,1.01074   ,74.0},
    {0.0,    28.7621   ,1.67191   ,15.7189   ,9.09227   , 14.5564   ,0.350500   ,5.44174   ,52.0861   ,10.4720   ,75.0},
    {0.0,    28.1894   ,1.62903   ,16.1550   ,8.97948   , 14.9305   ,0.382661   ,5.67589   ,48.1647   ,11.0005   ,76.0},
    {0.0,    30.4190   ,1.37113   ,15.2637   ,6.84706   , 14.7458   ,0.165191   ,5.06795   ,18.0030   ,6.49804   ,76.0},
    {0.0,    27.3049   ,1.59279   ,16.7296   ,8.86553   , 15.6115   ,0.417916   ,5.83377   ,45.0011   ,11.4722   ,77.0},
    {0.0,    30.4156   ,1.34323   ,15.8620   ,7.10909   , 13.6145   ,0.204633   ,5.82008   ,20.3254   ,8.27903   ,77.0},
    {0.0,    30.7058   ,1.30923   ,15.5512   ,6.71983   , 14.2326   ,0.167252   ,5.53672   ,17.4911   ,6.96824   ,77.0},
    {0.0,    27.0059   ,1.51293   ,17.7639   ,8.81174   , 15.7131   ,0.424593   ,5.78370   ,38.6103   ,11.6883   ,78.0},
    {0.0,    29.8429   ,1.32927   ,16.7224   ,7.38979   , 13.2153   ,0.263297   ,6.35234   ,22.9426   ,9.85329   ,78.0},
    {0.0,    30.9612   ,1.24813   ,15.9829   ,6.60834   , 13.7348   ,0.168640   ,5.92034   ,16.9392   ,7.39534   ,78.0},
    {0.0,    16.8819   , 0.4611   ,18.5913   , 8.6216   , 25.5582   , 1.4826   , 5.8600   ,36.3956   ,12.0658   ,79.0},
    {0.0,    28.0109   ,1.35321   ,17.8204   ,7.73950   , 14.3359   ,0.356752   ,6.58077   ,26.4043   ,11.2299   ,79.0},
    {0.0,    30.6886   ,1.21990   ,16.9029   ,6.82872   , 12.7801   ,0.212867   ,6.52354   ,18.6590   ,9.09680   ,79.0},
    {0.0,    20.6809   , 0.5450   ,19.0417   , 8.4484   , 21.6575   , 1.5729   , 5.9676   ,38.3246   ,12.6089   ,80.0},
    {0.0,    25.0853   ,1.39507   ,18.4973   ,7.65105   , 16.8883   ,0.443378   ,6.48216   ,28.2262   ,12.0205   ,80.0},
    {0.0,    29.5641   ,1.21152   ,18.0600   ,7.05639   , 12.8374   ,0.284738   ,6.89912   ,20.7482   ,10.6268   ,80.0},
    {0.0,    27.5446   ,0.655150   ,19.1584   ,8.70751   , 15.5380   ,1.96347   ,5.52593   ,45.8149   ,13.1746   ,81.0},
    {0.0,    21.3985   ,1.47110   ,20.4723   ,0.517394   , 18.7478   ,7.43463   ,6.82847   ,28.8482   ,12.5258   ,81.0},
    {0.0,    30.8695   ,1.10080   ,18.3841   ,6.53852   , 11.9328   ,0.219074   ,7.00574   ,17.2114   ,9.80270   ,81.0},
    {0.0,    31.0617   , 0.6902   ,13.0637   , 2.3576   , 18.4420   , 8.6180   , 5.9696   ,47.2579   ,13.4118   ,82.0},
    {0.0,    21.7886   ,1.33660   ,19.5682   ,0.488383   , 19.1406   ,6.77270   ,7.01107   ,23.8132   ,12.4734   ,82.0},
    {0.0,    32.1244   ,1.00566   ,18.8003   ,6.10926   , 12.0175   ,0.147041   ,6.96886   ,14.7140   ,8.08428   ,82.0},
    {0.0,    33.3689   , 0.7040   ,12.9510   , 2.9238   , 16.5877   , 8.7937   , 6.4692   ,48.0093   ,13.5782   ,83.0},
    {0.0,    21.8053   ,1.23560   ,19.5026   ,6.24149   , 19.1053   ,0.469999   ,7.10295   ,20.3185   ,12.4711   ,83.0},
    {0.0,    33.5364   ,0.916540   ,25.0946   ,0.039042   , 19.2497   ,5.71414   ,6.91555   ,12.8285   ,-6.7994   ,83.0},
    {0.0,    34.6726   ,0.700999   ,15.4733   ,3.55078   , 13.1138   ,9.55642   ,7.02588   ,47.0045   ,13.6770   ,84.0},
    {0.0,    35.3163   ,0.685870   ,19.0211   ,3.97458   , 9.49887   ,11.3824   ,7.42518   ,45.4715   ,13.7108   ,85.0},
    {0.0,    35.5631   , 0.6631   ,21.2816   , 4.0691   ,  8.0037   ,14.0422   , 7.4433   ,44.2473   ,13.6905   ,86.0},
    {0.0,    35.9299   ,0.646453   ,23.0547   ,4.17619   , 12.1439   ,23.1052   ,2.11253   ,150.645   ,13.7247   ,87.0},
    {0.0,    35.7630   ,0.616341   ,22.9064   ,3.87135   , 12.4739   ,19.9887   ,3.21097   ,142.325   ,13.6211   ,88.0},
    {0.0,    35.2150   ,0.604909   ,21.6700   ,3.57670   , 7.91342   ,12.6010   ,7.65078   ,29.8436   ,13.5431   ,88.0},
    {0.0,    35.6597   ,0.589092   ,23.1032   ,3.65155   , 12.5977   ,18.5990   ,4.08655   ,117.020   ,13.5266   ,89.0},
    {0.0,    35.1736   ,0.579689   ,22.1112   ,3.41437   , 8.19216   ,12.9187   ,7.05545   ,25.9443   ,13.4637   ,89.0},
    {0.0,    35.5645   ,0.563359   ,23.4219   ,3.46204   , 12.7473   ,17.8309   ,4.80703   ,99.1722   ,13.4314   ,90.0},
    {0.0,    35.1007   ,0.555054   ,22.4418   ,3.24498   , 9.78554   ,13.4661   ,5.29444   ,23.9533   ,13.3760   ,90.0},
    {0.0,    35.8847   ,0.547751   ,23.2948   ,3.41519   , 14.1891   ,16.9235   ,4.17287   ,105.251   ,13.4287   ,91.0},
    {0.0,    36.0228   , 0.5293   ,23.4128   , 3.3253   , 14.9491   ,16.0927   , 4.1880   ,100.613   ,13.3966   ,92.0},
    {0.0,    35.5747   ,0.520480   ,22.5259   ,3.12293   , 12.2165   ,12.7148   ,5.37073   ,26.3394   ,13.3092   ,92.0},
    {0.0,    35.3715   ,0.516598   ,22.5326   ,3.05053   , 12.0291   ,12.5723   ,4.79840   ,23.4582   ,13.2671   ,92.0},
    {0.0,    34.8509   ,0.507079   ,22.7584   ,2.89030   , 14.0099   ,13.1767   ,1.21457   ,25.2017   ,13.1665   ,92.0},
    {0.0,    36.1874   ,0.511929   ,23.5964   ,3.25396   , 15.6402   ,15.3622   ,4.18550   ,97.4908   ,13.3573   ,93.0},
    {0.0,    35.7074   ,0.502322   ,22.6130   ,3.03807   , 12.9898   ,12.1449   ,5.43227   ,25.4928   ,13.2544   ,93.0},
    {0.0,    35.5103   ,0.498626   ,22.5787   ,2.96627   , 12.7766   ,11.9484   ,4.92159   ,22.7502   ,13.2116   ,93.0},
    {0.0,    35.0136   ,0.489810   ,22.7286   ,2.81099   , 14.3884   ,12.3300   ,1.75669   ,22.6581   ,13.1130   ,93.0},
    {0.0,    36.5254   ,0.499384   ,23.8083   ,3.26371   , 16.7707   ,14.9455   ,3.47947   ,105.980   ,13.3812   ,94.0},
    {0.0,    35.8400   ,0.484938   ,22.7169   ,2.96118   , 13.5807   ,11.5331   ,5.66016   ,24.3992   ,13.1991   ,94.0},
    {0.0,    35.6493   ,0.481422   ,22.6460   ,2.89020   , 13.3595   ,11.3160   ,5.18831   ,21.8301   ,13.1555   ,94.0},
    {0.0,    35.1736   ,0.473204   ,22.7181   ,2.73848   , 14.7635   ,11.5530   ,2.28678   ,20.9303   ,13.0582   ,94.0},
    {0.0,    36.6706   ,0.483629   ,24.0992   ,3.20647   , 17.3415   ,14.3136   ,3.49331   ,102.273   ,13.3592   ,95.0},
    {0.0,    36.6488   ,0.465154   ,24.4096   ,3.08997   , 17.3990   ,13.4346   ,4.21665   ,88.4834   ,13.2887   ,96.0},
    {0.0,    36.7881   ,0.451018   ,24.7736   ,3.04619   , 17.8919   ,12.8946   ,4.23284   ,86.0030   ,13.2754   ,97.0},
    {0.0,    36.9185   ,0.437533   ,25.1995   ,3.00775   , 18.3317   ,12.4044   ,4.24391   ,83.7881   ,13.2674   ,98.0}
};

const double TBM[98+1] = {
    0.0,
    1.0079  ,4.0026  ,6.94    ,9.01218 ,10.81   ,12.011,
    14.0067 ,15.999  ,18.9984 ,20.17   ,22.98977,24.305,
    26.98154,28.08   ,30.97376,32.06   ,35.453  ,39.94,
    39.09   ,40.08   ,44.9559 ,47.9    ,50.941  ,51.996,
    54.9380 ,55.84   ,58.9332 ,58.7    ,63.54   ,65.377,
    69.72   ,72.5    ,74.9216 ,78.9    ,79.904  ,83.80,
    85.467  ,87.62   ,88.9059 ,91.22   ,92.9064 ,95.9,
    98.9062 ,101.0   ,102.9055,106.4   ,107.868 ,112.40,
    114.82  ,118.6   ,121.70  ,127.60  ,126.9045,131.30,
    132.9054,137.30  ,138.905 ,140.12  ,140.9077,144.20,
    147.    ,150.40  ,151.96  ,157.20  ,158.9254,162.50,
    164.9304,167.20  ,168.9342,173.0   ,174.97  ,178.4,
    180.947 ,183.8   ,186.2   ,190.2   ,192.2   ,195.0,
    196.9665,200.5   ,204.3   ,207.2   ,208.9804,210.0,
    210.0    ,222.0    ,223.0    ,226.0254,227.0    ,232.0381,
    231.0359,238.029 ,237.0482,242.0    ,243.0    ,247.0,
    247.0    ,249.0
};


//                          *** COMPTON TABLE ***
//-----THE VALUES OF THE COSTANTS FOR  COMPTON SCATTERING ARE TAKEN
//     FROM:
//     V.H. SMITH JR. A.J. THAKKAR AND C. CHAPMAN
//     ACTA CRYST. (1975), A31, 391.

const double TCS[95+1][4+1] = {
    { 0.0,0.0,0.0,0.0,0.0},
    //                    -----H -----
    { 0.0,    0.65390,-0.18456,8.2356,12.438},
    //                    -----HE-----
    { 0.0,    0.72391,-0.21464,9.1019,15.566},
    //                    -----LI-----
    { 0.0,    26.076,6.8817,26.299,0.88062},
    //                    -----BE-----
    { 0.0,    16.966,1117.7,40.948,103.99},
    //                    -----B -----
    { 0.0,    7.2997,272.81,22.693,34.403},
    //                    -----C -----
    { 0.0,    4.3353,93.125,14.656,14.628},
    //                    -----N -----
    { 0.0,    4.5051,40.965,11.061,7.3225},
    //                    -----O -----
    { 0.0,    3.2434,19.377,8.2735,4.0087},
    //                    -----F -----
    { 0.0,    2.7771,10.031,6.5952,2.3142},
    //                     -----NE-----
    { 0.0,    3.1880,5.6531,5.7748,1.3790},
    //                     -----NA-----
    { 0.0,    16.285,45.169,13.167,5.4569},
    //                     -----MG-----
    { 0.0,    115.98,3227.0,70.762,77.161},
    //                     -----AL-----
    { 0.0,    107.62,2740.9,67.151,59.872},
    //                     -----SI-----
    { 0.0,    80.719,1508.4,52.054,36.898},
    //                     -----P -----
    { 0.0,    55.933,703.48,37.402,20.872},
    //                     -----S -----
    { 0.0,    43.695,416.47,30.100,13.430},
    //                     -----CL-----
    { 0.0,    34.592,252.66,24.481,8.8480},
    //                     -----AR-----
    { 0.0,    27.153,150.48,19.697,5.8774},
    //                     -----K -----
    { 0.0,    30.545,193.71,21.865,6.5615},
    //                     -----CA-----
    { 0.0,    36.945,289.98,25.950,8.1578},
    //                     -----SC-----
    { 0.0,    33.212,232.59,23.472,6.8604},
    //                     -----TI-----
    { 0.0,    29.486,182.05,20.962,5.7117},
    //                     -----V -----
    { 0.0,    26.193,143.17,18.683,4.8164},
    //                     -----CR-----
    { 0.0,    18.929,72.508,13.819,3.0415},
    //                     -----MN-----
    { 0.0,    21.292,94.848,15.188,3.6204},
    //                     -----FE-----
    { 0.0,    19.276,77.511,13.788,3.1076},
    //                     -----CO-----
    { 0.0,    17.616,64.657,12.621,2.7056},
    //                     -----NI-----
    { 0.0,    16.346,55.896,11.687,2.4272},
    //                     -----CU-----
    { 0.0,    12.851,33.834,9.3436,1.7194},
    //                     -----ZN-----
    { 0.0,    14.619,45.462,10.338,2.0820},
    //                     -----GA-----
    { 0.0,    17.176,64.6920,11.820,2.5707},
    //                     -----GE-----
    { 0.0,    19.343,83.421,13.080,2.9211},
    //                     -----AS-----
    { 0.0,    19.471,84.720,13.126,2.8165},
    //                     -----SE-----
    { 0.0,    20.359,92.997,13.650,2.8488},
    //                     -----BR-----
    { 0.0,    20.316,92.534,13.623,2.7028},
    //                     -----KR-----
    { 0.0,    18.986,80.312,12.812,2.3584},
    //                     -----RB-----
    { 0.0,    21.269,101.61,14.209,2.6226},
    //                     -----SR-----
    { 0.0,    24.497,135.93,16.192,3.0174},
    //                     -----Y -----
    { 0.0,    24.539,136.20,16.237,2.9014},
    //                     -----ZR-----
    { 0.0,    23.692,126.53,15.732,2.6710},
    //                     -----NB-----
    { 0.0,    19.178,81.654,12.952,19.678},
    //                     -----MO-----
    { 0.0,    17.443,67.055,11.867,1.6907},
    //                     -----TC-----
    { 0.0,    18.256,73.766,12.363,1.7500},
    //                     -----RU-----
    { 0.0,    15.431,51.934,10.601,1.3610},
    //                     -----RH-----
    { 0.0,    14.236,43.914,9.8385,1.1954},
    //                     -----PD-----
    { 0.0,    12.125,31.377,8.4946,0.94341},
    //                     -----AG-----
    { 0.0,    11.910,30.295,8.3388,0.90612},
    //                     -----CD-----
    { 0.0,    12.283,32.428,8.5514,0.93212},
    //                     -----IN-----
    { 0.0,    12.893,36.009,8.9130,0.98158},
    //                     -----SN-----
    { 0.0,    13.547,40.031,9.3037,1.0326},
    //                     -----SB-----
    { 0.0,    13.486,39.657,9.2631,1.0057},
    //                     -----TE-----
    { 0.0,    14.490,46.190,9.8671,1.0869},
    //                     -----I -----
    { 0.0,    14.975,49.498,10.161,1.1116},
    //                     -----XE-----
    { 0.0,    14.885,48.883,10.102,1.0775},
    //                     -----CS-----
    { 0.0,    16.159,57.996,10.886,1.1773},
    //                     -----BA-----
    { 0.0,    17.835,71.158,11.921,13.128},
    //                     -----LA-----
    { 0.0,    18.225,74.363,12.167,1.3191},
    //                     -----CE-----
    { 0.0,    17.590,69.295,11.741,1.2501},
    //                     -----PR-----
    { 0.0,    16.613,61.934,11.071,1.1678},
    //                     -----ND-----
    { 0.0,    16.148,58.598,10.747,1.1195},
    //                     -----PM-----
    { 0.0,    15.741,55.777,10.458,10.774},
    //                     -----SM-----
    { 0.0,    15.359,53.212,10.182,1.0396},
    //                     -----EU-----
    { 0.0,    15.027,51.053,9.9393,1.0059},
    //                     -----GD-----
    { 0.0,    14.888,50.133,9.8426,0.97728},
    //                     -----TB-----
    { 0.0,    14.449,47.248,9.5464,0.93253},
    //                     -----DY-----
    { 0.0,    13.795,43.137,9.1027,0.87906},
    //                     -----HO-----
    { 0.0,    13.447,41.045,8.8604,0.84486},
    //                     -----ER-----
    { 0.0,    13.146,39.287,8.6467,0.81464},
    //                     -----TM-----
    { 0.0,    12.882,37.803,8.4552,0.78869},
    //                     -----YB-----
    { 0.0,    12.670,36.643,8.2968,0.76627},
    //                     -----LU-----
    { 0.0,    12.576,36.129,8.2264,0.74720},
    //                     -----HF-----
    { 0.0,    12.392,35.077,8.1062,0.72092},
    //                     -----TA-----
    { 0.0,    11.966,32.657,7.8420,0.67752},
    //                     -----W -----
    { 0.0,    11.429,29.720,7.5123,0.62773},
    //                     -----RE-----
    { 0.0,    10.841,26.660,7.1515,0.57682},
    //                     -----OS-----
    { 0.0,    10.553,25.229,6.9734,0.54767},
    //                     -----IR-----
    { 0.0,    10.255,23.781,6.7889,0.51891},
    //                     -----PT-----
    { 0.0,    9.1444,18.754,6.1116,0.43980},
    //                     -----AU-----
    { 0.0,    8.6692,16.793,5.8176,0.40417},
    //                     -----HG-----
    { 0.0,    8.8187,17.414,5.9035,0.40768},
    //                     -----TL-----
    { 0.0,    9.0707,18.476,6.0519,0.41721},
    //                     -----PB-----
    { 0.0,    9.3622,19.739,6.2252,0.42863},
    //                     -----BI-----
    { 0.0,    9.5574,20.605,6.3414,0.43371},
    //                     -----PO-----
    { 0.0,    9.9256,22.287,6.5624,0.44878},
    //                     -----AT-----
    { 0.0,    10.267,23.902,6.7681,0.46167},
    //                     -----RN-----
    { 0.0,    10.425,24.663,6.8643,0.46342},
    //                     -----FR-----
    { 0.0,    11.077,27.947,7.2583,0.49341},
    //                     -----RA-----
    { 0.0,    11.906,32.420,7.7600,0.53275},
    //                     -----AC-----
    { 0.0,    12.372,35.070,8.0440,0.55039},
    //                     -----TH-----
    { 0.0,    12.708,37.029,8.2501,0.56015},
    //                     -----PA-----
    { 0.0,    11.931,32.573,7.7684,0.51436},
    //                     -----U------
    { 0.0,    11.596,30.746,7.5590,0.49185},
    //                     -----NP-----
    { 0.0,    11.277,29.055,7.3579,0.47097},
    //                     -----PU-----
    { 0.0,    10.569,25.479,6.9123,0.43325},
    //                     -----AM-----
    { 0.0,    10.243,23.918,6.7044,0.41402}
};

class DC
{
public:
    double SAVE[99+1][6+1];
    int NSAVE;
};

class REFLS
{
public:
    int IREFS[IRS+1];
    double REFS[IRS+1][3+1];
    double FMGNTD[IRS+1];
    int ICR[99+1];
    double HALFL[IRS+1];
    double HALFG[IRS+1];
    double GAM[IRS+1];
    double FWHM[IRS+1][2+1];
};

class G1
{
public:
    double THMIN;
    double STEP;
    double THMAX;
    double U;
    double V;
    double W;
    double LAMDA[2+1];
    double TMV;
    double CTHM;
    double RLIM;
    double BKPOS; // sbx
    double WDT;
    double ULOR;
    double VLOR;
    double ZZZ;
    double UC;
};

class CNTRLS
{
public:
    int JOBTYP,NPROF,NPHASE,IOT,IPL,IPC,MAT,NXT,LST1,LST2,LST3,IPL1,IPL2,IPLST,IPLOSS,IPLCAL,IPLPOL,IPLCOM,IPLDIS,IPLAM,IPBIG,MCYCLE;
    double EPS;
    int MAXS, INSTRM,MAXSX,MCYCLX,ICYRUN,IPREF,IABSR;
    int FONDO;
    int IAS,IASYM;
    double SW;
    int IBGD,IDATA,ISPHASE,I2D94;
};

class PARAMS
{
public:
    double GLB[20 +1];
    double AGLB[20 +1];
    int LGLB[20 +1];
    double PAR[99 +1][30 +1];
    double APAR[99 +1][30 +1];
    int LPAR[99 +1][30 +1];
    double XL[NATS][11 +1];
    int LP[NATS][11 +1];
    double A[NATS][11 +1];
    int PTR[NATS +1];
    double RELAX[4 +1];
    double RATIO[2 +1];
};

class PARAC
{
public:
    string ATEXT[NATS+1];
    string NTYP[NATS+1];
};

class JNK
{
public:
    double ALOW[100+1];
    double AHIGH[100+1];
    double POS[100+1];
    double BCK[100+1];
    int NATOM[99+1];
    int NMOL[99+1];
    double PREF[99+1][3+1];
    int NBCKGD;
    int NEXCRG;
    int NSCAT; //nscatx
};

class ALLP
{
public:
    double FINAL[NFINAL+1][2+1];
    int ILOC;
};

class CHAR
{
public:
    string SYMB[99+1];
    string TITLE;
    string PHSNM[99+1];
};

class COEFC
{
public:
    string NAM[16+1];
};



class CNV
{
public:
    int N;
    double SINLAM[30+1];
    double F[30+1];

    void CV1(double AB[],double V[]);
    void CV2(double AB[],double* G);
    void STEEP(double X[], int N, int M);
};

class COEFF
{
public:
    double AC[10+1][16+1];
    double POSI[30+1];
    double SCAT[30+1];
    double DFP[16+1];
    double DFPP[16+1];
    double XMAS[16+1];
    CNV cnv;

    void COEF(int* J, int* K);
};

class BKGSCALE
{
public:
    double SCABKG[99+1];
};

class DATAX
{
public:
    double Y[IDSZ+1];
    double VAR[IDSZ+1];
    double YC[IDSZ+1];
    int KR[IDSZ+1];  // phs
    double BK[IDSZ+1];
    int NPTS;
    double AMORPHOUS[IDSZ+1];
};

class RTMTX
{
public:
    int IVEC[99+1][192+1];
    int MLTPHS[99+1];
    int ICNTPHS[99+1];
};

class SIZESTRAIN
{
public:
    double SIZEG[15+1];
    double STRAING[15+1];
    double SIZEL[15+1];
    double STRAINL[15+1];
    double SIZ[15+1];
    double STRAIN[15+1];
    int NSIZESTRAIN;
};

class CONVERT
{
public:
    int ICNVT;
};



class SPGCOM
{
public:
    ofstream* outputfile;

    int NSPGRP;
    int NAXIS;
    int NCONT[10+1];
    int NC;
    int NPOL;
    int MULTP; // multpommon

    void SPGP(string SPG);
private:
    void GOTOER(void);

    // TODO: talvez mudar LAU para ca
    /*
    // Laue classes
    const string LAU[14] = {
        "1BAR",			// Triclini
        "2/M",			// Monoclinic
        "MMM",			// Orthorhombic
        "4/M",			// Tetragonal
        "4/MMM",		// "
        "3BAR   R",		// Trigonal, romboedric
        "3BAR M R",		// Trigonal
        "3BAR",			// Trigonal
        "3BAR M 1",		// Trigonal
        "3BAR 1 M",
        "6/M",			// Hexagonal
        "6/MMM",		// Hexagonal
        "M3",			// Cubico
        "M3M"			// Cubico
    };
    */

};

class VOLUME
{
public:
    double VOLI[99+1];
    double GCOM[99+1];
};

class G2
{
public:
    double S1,S2,SS2,S3,SS4,D1,D2,D4,R1,R2,R3,R2NOBK,R3NOBK;
};

class F1
{
public:
    double RJAC[MSZ+1][MSZ+1]; // SMM
    double VX[MSZ+1]; // V1, V
};

class G3
{
public:
    double COND;
    int IORD1;
    int IORD2;
    double TH;
    int NUM;
};

class FONDI
{
public:
    double BKCOM[IDSZ+1];
    double BKDIS[IDSZ+1];
    double BKAM[IDSZ+1];
    double BKPOL[IDSZ+1];
};

class BLNK1
{
public:
    int N,NMAX;
    int MULTX; // L
    double Y[192+1][3+1];			//  X[192+1],Y[192+1],Z[192+1];
    double XLT;
    int IXB;   // kxb
    int NCTR[192+1];
};

class MULTIP
{
public:
    double TMASSA[99+1];
    int MLTPHASE;
    double XMLTP[99+1];
    int MURT[NATS+1];
    double SAQF[99+1];
    double WTIS[99+1];
};

class SIMOPER
{
public:
    int ISIMOP;
};

class HKLCTL
{
public:
    int IHKL[3+1][48+1];
    double AZ[48+1];
    int NC1[10+1][7+1][99+1];
    int ICHKL[99+1];
    int N1HKL[99+1];
    int IER;
};

class CELLX
{
public:
    double A; // AA
    double B;
    double C;
    double ALPHA;
    double BETA;
    double GAMMA;
    double AL[3+1][3+1];
};

class PRFX
{
public:
    double DELT;
    double TL;
    double GAM1;
    double GAM2;
    double PRFDER;
    int IPH;
    double DELTA;
};

class PVII
{
public:
    double TF1,TF2,TF4,TF6,TF8,TF9,C4;
};

class SPVII
{
public:
    double RL,DA1L,DA2L,DA3L,DA4L,DA5L,DA6L,DA7L,
        RH,DA1H,DA2H,DA3H,DA4H,DA5H,DA6H,DA7H;
};

class STRUPHASE
{
public:
    double APHASE[IRS+1];
    double TAVIX[IRS+1];
    double SRIX[IRS+1];
};

class MAXINT
{
public:
    double XMAXINT;
};

class CODEBCK
{
public:
    int IBCKCODE;
};

class G4
{
public:
    double TANN[NOV+1];
    double DERSTO[NOV+1][MSZ+1];
};

class COMP
{
public:
    double CC[4+1][16+1];
    double ZEFF[16+1];
    int PTC[NATS+1];
};

class ATFAT
{
public:
    double FI2[2+1];
};

class DIRCV
{
public:
    double DCSM[6+1][6+1];
    double DCV[6+1];
};

class LABELS
{
public:
    int LB1,LB2,LB3;
    string TEST;
};

class DBWSException {
public:
    DBWSException(string m)
    {
        msg = m;
    };
    ~DBWSException() {};
    const string Show() const
    {
        return msg;
    }
private:
    string msg;
};

class DBWS
{
public:
    DBWS(void);
    virtual ~DBWS(void);
    void run(void);

public:
    DC* dc;
    REFLS* refls;
    G1* g1;
    CNTRLS* cntrls;
    PARAMS* params;
    PARAC* parac;
    JNK* jnk;
    ALLP* allp;
    CHAR* char_;
    COEFC* coefc;
    COEFF* coeff;
    BKGSCALE* bkgscale;
    DATAX* datax;
    RTMTX* rtmtx;
    SIZESTRAIN* sizestrain;
    CONVERT* convert;
    SPGCOM* spgcom;
    VOLUME* volume;
    G2* g2;
    F1* f1;
    G3* g3;
    FONDI* fondi;
    BLNK1* blnk1;
    MULTIP* multip;
    SIMOPER* simoper;
    HKLCTL* hklctl;
    CELLX* cellx;
    PRFX* prfx;
    PVII* pvii;
    SPVII* spvii;
    STRUPHASE* struphase;
    MAXINT* maxint;
    CODEBCK* codebck;
    G4* g4;
    COMP* comp;
    ATFAT* atfat;
    DIRCV* dircv;
    LABELS* labels;

    ifstream file3;
    string file3name;

    ifstream file4;
    ofstream file4b;

    string file4name;

    ifstream file5;
    ofstream file5b;

    string file5name;

    ofstream file6;
    string file6name;

    ofstream file7;

    ofstream file8o;
    ifstream file8i;
    string file8name;

    ifstream file11;
    string file11name;

    ofstream file20;

    ofstream file53; // at INPTR

public:
    int sign(int i);
    double sign(double i);
    void GOTOER(void);


    void SORT(int IPHASE);
    void ASSIGN_(void);
    void CHISQ(void);
    void COMPTON(int K, double STH, double* CISK);
    void DISORDER(int K, double STH, int IDERIV, double* SDK, double* DYC, int FONDO, double DERISO[]);
    void SMTRY2(int* IPHASE);
    void CALCUL(int NN);
    void PRSVII(double T);
    double gamma(double X);
    void mspvii(double A, double W);
    double PROFIL(int N, double X);
    void SUMMAT(int IPM, double CSK[], double DISK[], double DYCDD[], double ISODER[], double TOTCS);
    double DPINV(double A1[][MSZ+1], double B1[], int* N);
    double ERROR(double A[][6+1], double B[], double* OMEGA);
    void ESD(double SM[][6+1], double V[], double SUM);
    void DIRECT(double SM[][6+1], double V[], int* IPH);
    void OUTSCR(int I, double R2, double R3, double X);
    void OUTPTR(int ICYCLE);
    void ITER(void);
    void CEL000(void);
    void OPERTR(int* I, int* L2);
    void SYMOPR(void);
    void RTMT(int* IPRT, int* IPHASE);
    void OP1(int* IPHASE);
    void LOOKUP(int K, int N, int NSCAT, int IXRAY, int JOB);
    void CELL2(int NPHASE, double LAMDAM);
    double MULT(int IH, int IK, int IL, int KXIS);
    void REWRIT(int ISCALE, int IDIF);
    void size(int K);
    void WRITE94(int ISCALE, int IDIF);
    void EXPUT(void);
    void REFGEN(int IPHASE, double ZERO, double DIS, double TRANS, double PREF[][3+1], double PREFOR);
    void FINDC(int K, int NSCAT);
    void ABSORP(double MU, double SW, double TH, double* ABC);
    void conv94(void);
    void GSASREAD(void);
    void PHILIPSREAD(void);
    void qpainit(void);
    void rigakuread(void);
    void readasc(void);
    void scintag(void);
    void SIEMENSREAD(void);
    void INPTR(void);
    void inpam(void);

};


void CNV::CV1(double AB[],double V[])
{
    int I,J,K,MAXX,NN;

    MAXX=min(9,N);
    for (K=1; K <= MAXX; K = K + 2)
    {
        V[K]=0;
        for (I=1; I <= N; ++I)
        {
            V[K]=V[K]+F[I];
            for (J=1; J <= MAXX; J = J + 2) V[K]=V[K]-AB[J]*exp(-AB[J+1]*SINLAM[I]);
        }
        V[K]=-2.0*V[K]*exp(-AB[K+1]*SINLAM[K]);
        V[K+1]=-V[K]*SINLAM[K]*AB[K];
    }
    NN=MAXX+1;
    for (I=NN; I <= 10; ++I) V[I]=0;
}

void CNV::CV2(double AB[],double* G)
{
    int I,J,MAXX;
    double T;

    MAXX=min(9,N);
    *G=0;
    for (I=1; I <= N; ++I)
    {
        T=F[I];
        for (J=1; J <= MAXX; J = J + 2) T=T-AB[J]*exp(-AB[J+1]*SINLAM[I]);
        *G=*G+T*T;
    }
}

void CNV::STEEP(double X[], int N, int M)
{
    int I,J,K;
    double Z[16+1],T[16+1],ZNT[16+1],SIGT[16+1],SIG[16+1],Y[16+1][16+1],XI[16+1],ZN[16+1];
    double ALPHA,D,G,G1,U,V,W,T1,T2,ETA;

    if (N == 0)
    {
        CV1(X,Z);
        CV2(X,&G);
        for (I=1; I <= M; ++I)
        {
            for (J=1; J <= M; ++J)
            {
                Y[I][J]=0.0;
                if (I == J) Y[I][J]=1.0;
            }
        }
        for (K=1; K <= N; ++K)
        {
            U=0;
            for (I=1; J <= M; ++J)
            {
                T1=0;
                for (J=1; J <= M; ++J) T1=T1+Z[J]*Y[I][J];
                SIG[I]=T1;
                U=U-SIG[I]*Z[I];
            }
            ETA=min(1.0,abs(2.0*G/U));
            for (I=1; I <= M; ++I) T[I]=X[I]-ETA*SIG[I];
            CV1(T,ZN);
            V=0.0;
            for (I=1; I <= M; ++I) V=V-ZN[I]*SIG[I];
            CV2(T,&G1);
            D=3.0*abs(G-G1)/ETA+U+V;
            W=sqrt(D*D-U*V);
            ALPHA=ETA*(1.0-(V+W-D)/(V-U+2.0*W));
            for (I=1; I <= M; ++I)
            {
                SIG[I]=-1.0*ALPHA*SIG[I];
                X[I]=X[I]+SIG[I];
                ZN[I]=Z[I];
            }
            CV1(X,Z);
            CV2(X,&G);
            T1=0;
            for (I=1; I <= M; ++I)
            {
                XI[I]=Z[I]-ZN[I];
                T1=T1+XI[I]*SIG[I];
            }
            for (I=1; I <= M; ++I) SIGT[I]=SIG[I]/T1;
            T1=0;
            for (I=1; I <= M; ++I)
            {
                T2=0;
                for (J=1; J <= M; ++J) T2=T2+XI[J]*Y[I][J];
                ZN[I]=T2;
                T1=T1+ZN[I]*XI[I];
            }
            for (I=1; I <= M; ++I)
            {
                T2=0;
                for (J=1; J <= M; ++J) T2=T2+XI[J]*Y[J][I];
                ZNT[I]=T2/T1;
            }
            for (I=1; I <= M; ++I)
            {
                for (J=1; J <= M; ++J) Y[I][J]=Y[I][J]+SIG[I]*SIGT[J]-ZN[I]*ZNT[J];
            }
        }
    }
}

void  COEFF::COEF(int* J, int* K)
{
    int I,NN,NN1;
    double AB[10+1];

    for(I=1; I <= *K; ++I)
    {
        cnv.SINLAM[I]=POSI[I]*POSI[I];
        cnv.F[I]=SCAT[I];
    }
    cnv.N=*K;
    NN = min(cnv.N,9);
    for (I=1; I <= NN; ++I) AB[I]=I;
    NN1=NN+1;
    for (I=NN1; I <= 10; ++I) AB[I]=0.0;
    cnv.STEEP(AB,3,10);
    if (AB[3] == 0.0) AB[3]=-1E-6;
    for (I=1; I <= 9; ++I) AC[I][*J]=AB[I];
}







void SPGCOM::SPGP(string SPG)
{
    //
    //                             THIS SR INTERPRETS THE SPACE GROUP SYMBOL
    //                           AND GENERATES OPERATORS WHICH SYNSPGRPOPR USES TO
    //                           GENERATE THE FULL SET OF EQUIVALENT
    //                           POSITIONS AND OP1 AND SMTRY2 USE TO GENERATE
    //                           THE FULL SET OF EQUIVALENT INDICES.

    const int NDAT[7+1] = { 0,3,1,2,0,2,1,3};
    const string CHR = "  CBAPFIRMND123456-/H.";
    int I,J,K,M,M1,M2,M3,N,LD,IJ,KJ,MJ,IX,IN1,IN2,NI4,NI5,NI6,NK4,NK5,NK6,NM4,
        NM5,NM6,NCI,NCK,NCM,LOD,NIJ,NXC,NYC,NZC,NJX,ICO4,ICO5,ICO6,KCO4,KCO5,KCO6,
        MCO4,MCO5,IKO4,IKO5,IKO6,MCO6,LOD1;
    int L[4+1][4+1];
    int ICO[6+1][15+1];


    for (I=0; I <= 6; ++I)
    {
        for (J=0; J <= 15; ++J) ICO[I][J]=0;
    }
    for (I=0; I <= 4; ++I)
    {
        for (J=0; J <= 4; ++J) L[I][J]=0;
    }
    M2 = 0;
    K = 1;
    M = 1;
    NSPGRP = 0;
    NAXIS = 0;
    N = 0;

    for (J=1; J <= 20; ++J)
    {
        for (I=1; I <= 21; ++I)
        {
            if ( SPG[J] == CHR[I] )
            {
                if ( K+M+I == 3 ) goto L110;
                if ( I == 1 ) goto L108;
                L[M][K] = I;
                N = 0;
                M = M+1;
                if ( M-4 <= 0 ) goto L110; else goto L108;
                goto L108;
            }
        }
        goto L110;



L108:
        if ( N != 1 )
        {
            N = 1;
            M = 1;
            K = K+1;
            if ( K > 4 ) goto L200;
        }
        goto L110;

L110:;
    }

    /*
    for (J=1; J <= 20; ++J)
    {
        for (I=1; I <= 21; ++I)
        {
            if ( SPG[J] == CHR[I] ) goto L101;
        }
        goto L110;
L101:
        if ( K+M+I == 3 ) goto L110;
        if ( I == 1 ) goto L108;
        L[M][K] = I;
        N = 0;
        M = M+1;
        if ( M-4 <= 0 ) goto L110; else goto L108;
L108:
        if ( N == 1 ) goto L110;
        N = 1;
        M = 1;
        K = K+1;
        if ( K > 4 ) goto L200;
L110:;
    }
    */



L200:
    if ( K <= 2 ) goto L500;
    if ( L[1][1] > 8 ) goto L500;
    J = 1;
    I = L[1][1]-1;
    switch (I) {
    case 1:
        goto L203;
        break;
    case 2:
        goto L202;
        break;
    case 3:
        goto L201;
        break;
    case 4:
        goto L119;
        break;
    case 5:
        goto L204;
        break;
    case 6:
        goto L205;
        break;
    case 7:
        goto L206;
        break;
    }
    GOTOER();
L201:
    NCONT[J] = 576;
    goto L209;
L202:
    NCONT[J] = 516;
    goto L209;
L203:
    NCONT[J] = 68;
    goto L209;
L204:
    NCONT[J] = 576;
    J = J+1;
    goto L202;
L205:
    NCONT[J] = 580;
    goto L209;
L207:
    K = K-1;
    NSPGRP = K+3;
    goto L210;
L206:
    if ( L[1][3] == 8  || L[1][4] == 8 ) goto L207;
    NCONT[J] = 8192;
    if ( L[1][K-1] == 20 ) K=K-1;
    NSPGRP = K+5;
    J = J+1;
    goto L210;
L209:
    J = J+1;
L119:
    if ( K-4 < 0 )
    {
        goto L120;
    }
    else if ( K-4 == 0)
    {
        goto L130;
    }
    else
    {
        goto L140;
    }
L120:
    if ( L[1][2] == 18 ) goto L121;
    if ( L[1][2] == 17 ) goto L122;
    if ( L[1][2] == 14 ) goto L123;
    if ( L[1][2] == 15 ) goto L124;
    if ( L[1][2] == 12 ) goto L125;
    NSPGRP = 2;
    NAXIS = 2;
    goto L210;
L121:
    if ( L[2][2] == 17 ) goto L122;
    if ( L[2][2] == 14 ) goto L123;
    if ( L[2][2] == 15 ) goto L124;
    if ( L[2][2] == 12 ) goto L125;
    goto L500;
L122:
    NSPGRP = 11;
    goto L210;
L123:
    NSPGRP = 8;
    goto L210;
L124:
    NSPGRP = 4;
    goto L210;
L125:
    NSPGRP = 1;
    goto L210;
L130:
    if ( L[1][3] != 14 ) goto L500;
    NSPGRP = 13;
    goto L210;
L140:
    if ( L[1][3] == 14 ) goto L151;
    if ( L[1][2] == 18 ) goto L152;
    if ( L[1][2] == 17 ) goto L153;
    if ( L[1][2] == 14 ) goto L154;
    if ( L[1][2] == 15 ) goto L155;
    if ( L[1][2] == 12 ) goto L141;
    if ( L[1][3] == 12 ) goto L142;
    if ( L[1][4] == 12 ) goto L500;
    NSPGRP = 3;
    goto L210;
L141:
    if ( L[1][3] == 12 ) goto L143;
    if ( L[1][4] != 12 ) goto L500;
    NSPGRP = 2;
    NAXIS = 2;
    goto L210;
L142:
    if ( L[1][4] != 12 ) goto L500;
    NSPGRP = 2;
    NAXIS = 1;
    goto L210;
L143:
    if ( L[1][4] == 12 ) goto L500;
    NSPGRP = 2;
    NAXIS = 3;
    goto L210;
L151:
    NSPGRP = 14;
    goto L210;
L152:
    if ( L[2][2] == 17 ) goto L153;
    if ( L[2][2] == 14 ) goto L154;
    if ( L[2][2] == 15 ) goto L155;
    goto L500;
L153:
    NSPGRP = 12;
    goto L210;
L154:
    if ( L[1][3] == 12 ) goto L156;
    if ( L[1][4] != 12 ) goto L500;
    NSPGRP = 9;
    goto L210;
L155:
    NSPGRP = 5;
    goto L210;
L156:
    if ( L[1][4] == 12 ) goto L123;
    NSPGRP = 10;
L210:;
    K = K-1;

    //DB 2
    //     PRINT 4,K,NSPGRP,NAXIS,J,NCONT
    //   4 FORMAT ('0K =',I3,'  NSPGRP =',2I4,'  J=',I3/' NCONT =',10I4)
    N = 1;
    for (M=2; M <= K; ++M)
    {
        if ( L[1][M] == 0 ) goto L500;
L218:
        I = abs(L[1][M]-5);
L219:
        if ( I <= 0 || I > 15 ) goto L500;
        //C              A   B   C   M   N   D   1   2   3   4   5   6   -   /
        switch (I) {
        case 1:
            goto L220;
            break;
        case 2:
            goto L220;
            break;
        case 3:
            goto L220;
            break;
        case 4:
            goto L220;
            break;
        case 5:
            goto L220;
            break;
        case 6:
            goto L245;
            break;
        case 7:
            goto L250;
            break;
        case 8:
            goto L255;
            break;
        case 9:
            goto L260;
            break;
        case 10:
            goto L265;
            break;
        case 11:
            goto L500;
            break;
        case 12:
            goto L270;
            break;
        case 13:
            goto L275;
            break;
        case 14:
            goto L300;
            break;
        case 15:
            goto L300;
            break;
        }
        GOTOER();
        //        H
L220:
        switch (M) {
        case 1:
            goto L500;
            break;
        case 2:
            goto L221;
            break;
        case 3:
            goto L222;
            break;
        case 4:
            goto L223;
            break;
        }
        GOTOER();
L221:
        if ( L[1][3] == 14 ) goto L2230;
        if ( L[1][2] == 15 ) goto L2230;
        if ( L[1][2] == 17 ) goto L2230;
        if ( K == 2 ) goto L2220;
L2210:
        ICO[1][N] = 2;
        ICO[2][N] = 0;
        ICO[3][N] = 0;
        ICO[4][N] = 4;
        if ( I == 2 || I == 5 ) ICO[5][N] = 1;
        if ( I == 3 || I == 5 ) ICO[6][N] = 1;
L2211:
        N = N+1;
        if ( L[1][2] != 15 ) goto L300;
        ICO[1][N] = ICO[2][N-1];
        ICO[2][N] = ICO[1][N-1];
        ICO[3][N] = ICO[3][N-1];
        ICO[4][N] = ICO[5][N-1];
        ICO[5][N] = ICO[4][N-1];
        ICO[6][N] = ICO[6][N-1];
        N= N+1;
        goto L300;
L222:
        if ( L[1][2] == 14 || L[1][2] == 17 ) goto L225;
        if ( L[1][2] == 15 ) goto L2210;
L2220:
        ICO[1][N] = 0;
        ICO[2][N] = 2;
        ICO[3][N] = 0;
        ICO[5][N] = 4;
        if ( I == 1 || I == 5 ) ICO[4][N] = 1;
        if ( I == 3 || I == 5 ) ICO[6][N] = 1;
        N = N+1;
        goto L300;
L223:
        if ( L[1][3] == 14 || L[1][2] == 15 ) goto L224;
        if ( L[1][2] == 14 || L[1][2] == 17 ) goto L224;
L2230:
        ICO[1][N] = 0;
        ICO[2][N] = 0;
        ICO[3][N] = 2;
        ICO[6][N] = 4;
        if ( I == 1 || I == 5 ) ICO[4][N] = 1;
        if ( I == 2 || I == 5 ) ICO[5][N] = 1;
        N = N+1;
        if ( M == 2 && L[1][2] == 15 && I == 5 ) ICO[4][N-3]=1;
        if ( M !=  2 || L[1][2] != 17) goto L300;
        if ( L[1][3] == 2 ) ICO[6][N-1]=1;
        if ( L[1][4] == 2 ) ICO[6][N-1]= (ICO[6][N-1]+1) % 2;
        goto L300;
L224:
        ICO[1][N] = 1;
        ICO[2][N] = 1;
        ICO[3][N] = 0;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        if ( I == 3 || I == 5 ) ICO[6][N] = 1;
        if ( NSPGRP == 7 && I == 3 ) goto L2240;
        if ( I != 5 ) goto L226;
L2240:
        ICO[4][N] = 5;
        ICO[5][N] = 5;
        N = N+1;
        goto L300;
L225:
        if ( NSPGRP == 7 ) goto L224;
        ICO[1][N] = 3;
        ICO[2][N] = 3;
        ICO[3][N] = 0;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        if ( I == 3 ) ICO[6][N] = 1;
L226:
        N = N+1;
        goto L300;
L245:
        //    D TYPE MIRROR
        switch (M) {
        case 1:
            goto L500;
            break;
        case 2:
            goto L246;
            break;
        case 3:
            goto L247;
            break;
        case 4:
            goto L248;
            break;
        }
        GOTOER();
L246:
        if ( K == 2 ) goto L247;
        ICO[1][N] = 2;
        ICO[2][N] = 0;
        ICO[3][N] = 0;
        ICO[4][N] = 6;
        ICO[5][N] = 6;
        ICO[6][N] = 6;
        N = N+1;
        goto L300;
L247:
        ICO[1][N] = 0;
        ICO[2][N] = 2;
        ICO[3][N] = 0;
        ICO[4][N] = 6;
        ICO[5][N] = 6;
        ICO[6][N] = 6;
        N = N+1;
        goto L300;
L248:
        if ( L[1][2] == 15 || L[1][3] == 14 ) goto L249;
        ICO[1][N] = 0;
        ICO[2][N] = 0;
        ICO[3][N] = 2;
        ICO[4][N] = 6;
        ICO[5][N] = 6;
        ICO[6][N] = 6;
        N = N+1;
        goto L300;
L249:
        ICO[1][N] = 1;
        ICO[2][N] = 1;
        ICO[3][N] = 0;
        ICO[4][N] = 6;
        ICO[5][N] = 6;
        ICO[6][N] = 6;
        if (L[1][3] != 13) goto L226;
        ICO[4][N] = 0;
        ICO[5][N] = 1;
        N = N+1;
        goto L300;
L250:
        //    1 FOLD ROTATION
        if ( L[2][M] != 3 ) goto L300;
        ICO[1][N] = 2;
        ICO[2][N] = 2;
        ICO[3][N] = 2;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        ICO[6][N] = 4;
        N = N+1;
        goto L300;
L255:
        switch (M) {
        case 1:
            goto L500;
            break;
        case 2:
            goto L256;
            break;
        case 3:
            goto L257;
            break;
        case 4:
            goto L258;
            break;
        }
        GOTOER();
        //    2 FOLD ROTATION
L256:
        if ( K == 2 ) goto L2571;
L2561:
        ICO[1][N] = 0;
        ICO[2][N] = 2;
        ICO[3][N] = 2;
        ICO[4][N] = 0;
        ICO[5][N] = 4;
        ICO[6][N] = 4;
        if ( abs(L[2][M]-13) == 1 ) ICO[4][N] = 1;
L2560:
        N = N+1;
        for (I=2; I <= 4; ++I) if ( L[I][M] == 19 ) goto L2563;
        goto L300;
L2563:
        if ( L[I+1][M] <= 1 ) goto L500;
        I = abs(L[I+1][M]-5);
        goto L219;
L257:
        if ( L[1][2] == 14 || L[1][2] == 17 ) goto L259;
L2571:
        ICO[1][N] = 2;
        ICO[2][N] = 0;
        ICO[3][N] = 2;
        ICO[4][N] = 4;
        ICO[5][N] = 0;
        ICO[6][N] = 4;
        if ( L[2][M] == 12 ) ICO[5][N]=1;
        if ( L[1][2] == 15 ) goto L2211;
        goto L2560;
L258:
        if ( L[1][2] >= 14   ) goto L2595;
        if ( L[1][3] == 14 ) goto L259;
L2581:
        ICO[1][N] = 2;
        ICO[2][N] = 2;
        ICO[3][N] = 0;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        ICO[6][N] = 0;
        if ( abs(L[2][M]-13) == 1 ) ICO[6][N] = 1;
        if ( L[2][M] == 16 ) ICO[6][N] = 1;
        goto L2560;
L259:
        if ( L[1][3] == 8 || L[1][4] == 8 ) goto L2595;
        ICO[1][N] = 1;
        ICO[2][N] = 1;
        ICO[3][N] = 2;
        ICO[4][N] = 0;
        ICO[5][N] = 0;
        ICO[6][N] = 4;
        goto L2560;
L2595:
        if ( L[1][2] == 15)goto L259;
        ICO[1][N] = 3;
        ICO[2][N] = 3;
        ICO[3][N] = 2;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        ICO[6][N] = 4;
        goto L2560;
        //    3 FOLD ROTATION
L260:
        switch (M) {
        case 1:
            goto L500;
            break;
        case 2:
            goto L261;
            break;
        case 3:
            goto L262;
            break;
        case 4:
            goto L500;
            break;
        }
        GOTOER();
L261:
        if ( L[1][1] == 8 && J == 1 ) goto L262;
        IX = 0;
        if ( L[2][M] == 12 ) goto L2611;
        if ( L[2][M] == 13 ) goto L2612;
L2610:
        NCONT[J] = 8196;
        goto L2613;
L2611:
        NCONT[J] = 8200;
        if ( L[1][4] != 13 ) goto L2613;
        J = J+1;
        NCONT[J] = 2321;
        if ( L[1][2] == 14 ) NCONT[J]=4403;
        L[1][3] = 12;
        L[1][4] = 12;
        goto L2613;
L2612:
        NCONT[J] = 8204;
        if ( L[1][4] != 13 ) goto L2613;
        J = J+1;
        NCONT[J] = 4369;
        if ( L[1][2] == 14 ) NCONT[J]=2355;
        L[1][3] = 12;
        L[1][4] = 12;
L2613:
        J = J+1;
        if ( IX-1 < 0 )
        {
            goto L250;
        } else if ( IX-1 == 0)
        {
            goto L2581;
        }
        else
        {
            goto L2230;
        }
        //    CUBIC OR RHOMBOHEDERAL
L262:
        NCONT[J] = 16384;
        if (L[2][M] != 3 ) goto L2620;
        J = J+1;
        NCONT[J] = 290;
L2620:
        J = J+1;
        if ( N == 1 ) goto L300;
        I = N-1;
        IN1 = N;
        IN2 = N+1;
        ICO[1][IN1] = ICO[3][I];
        ICO[2][IN1] = ICO[1][I];
        ICO[3][IN1] = ICO[2][I];
        ICO[4][IN1] = ICO[6][I];
        ICO[5][IN1] = ICO[4][I];
        ICO[6][IN1] = ICO[5][I];
        ICO[1][IN2] = ICO[2][I];
        ICO[2][IN2] = ICO[3][I];
        ICO[3][IN2] = ICO[1][I];
        ICO[4][IN2] = ICO[5][I];
        ICO[5][IN2] = ICO[6][I];
        //L263:
        ICO[6][IN2] = ICO[4][I];
        N = IN2+1;
        goto L300;
L265:
        if ( M != 2 ) goto L500;
        ICO[1][N] = 3;
        ICO[2][N] = 1;
        ICO[3][N] = 0;
        ICO[4][N] = 4;
        ICO[5][N] = 4;
        ICO[6][N] = 0;
        if ( L[2][2] == 12 ) ICO[6][N] = 2;
        if ( L[2][2] == 13 ) ICO[6][N] = 1;
        if ( L[2][2] == 14 ) ICO[6][N] = 3;
        if ( L[2][2] ==  3 ) ICO[3][N] = 2;
        N = N+1;
        if ( L[2][2] == 3 && L[1][3] == 14 && L[1][4] == 11 ) goto L266;
        if ( L[1][3] == 14 ) goto L2561;
        if ( K > 2 || L[1][1] != 7 ) goto L2581;
        if ( L[2][2]+L[3][2] != 12 ) goto L2581;
        L[2][2] = 0;
        ICO[5][N-1] = 1;
        goto L2581;
L266:
        if ( L[1][1] != 7 || L[1][2] != 15 ) goto L500;
        NCONT[J] = 16384;
        NCONT[J+1] = 356;
        NCONT[J+2] = 834;
        NCONT[J+3] = 1177;
        J = J+3;
        goto L410;
L270:
        if ( M != 2 ) goto L500;
        IX = 1;
        if ( L[2][2] == 12 ) goto L2611;
        if ( L[2][2] == 13 ) goto L2612;
        if ( L[2][2] == 14 ) goto L2610;
        if ( L[2][2] == 15 ) goto L2611;
        if ( L[2][2] == 16 ) goto L2612;
        if ( L[2][2] ==  3 ) goto L271;
        goto L2610;
L271:
        IX = 2;
        goto L2610;
L275:
        if ( M != 2 ) goto L500;
        L[1][2] = L[2][2];
        L[2][2] = 3;
        goto L218;
L300:;
    }


    N = N-1;
    IJ = J;
    if ( N > 0 ) goto L301;
    J = J-1;
    goto L410;
L301:
    for (I=1; I <= N; ++I)
    {
        NCONT[J] = ICO[1][I]+16*ICO[2][I]+128*ICO[3][I]+4*(ICO[4][I] % 4) +64*(ICO[5][I] % 4)+512*(ICO[6][I] % 4);
        J = J+1;
    }
L306:
    J = J-1;
    //DB 4
    //     PRINT 5,J,IJ,N,NSPGRP,NAXIS,NCONT
    //    1 ,ICO
    //   5 FORMAT ('0J =',I3,'  IJ =',I3,'  N =',I3,'  NSP =',2I5/
    //    1 ' NCONT =',10I4/' ICO =',6I3/(6X6I3))
    //L310:
    for (I=IJ; I <= J; ++I)
    {
        NCI = I-IJ+1;
        ICO4 = ICO[4][NCI] % 4;
        ICO5 = ICO[5][NCI] % 4;
        ICO6 = ICO[6][NCI] % 4;
        NI4 = NDAT[ICO4+4];
        NI5 = NDAT[ICO5+4];
        NI6 = NDAT[ICO6+4];
        KJ = I+1;
        if ( (NCONT[I] & 307)-290 == 0) goto L315; else goto L320;
L315:
        if ( (NCONT[I] & 7884) == 0 ) goto L318; else goto L316;
        //   A CENTER IS PRESENT, NOT AT 0,0,0
L316:;
        *outputfile << endl << " A 1-bar site is present, But NOT at 0,0,0" << endl;
        goto L320;
        //   A CENTER AT 0,0,0 IS PRESENT
L318:;
L320:;
        if ( KJ > J ) goto L400;
        for (K=KJ; K <= J; ++K)
        {
            NCK = K-IJ+1;
            KCO4 = ICO[4][NCK] % 4;
            KCO5 = ICO[5][NCK] % 4;
            KCO6 = ICO[6][NCK] % 4;
            NK4 = NDAT[KCO4+4];
            NK5 = NDAT[KCO5+4];
            NK6 = NDAT[KCO6+4];
            NXC = NI4+NK4 % 4;
            NYC = NI5+NK5 % 4;
            NZC = NI6+NK6 % 4;
            MJ = K+1;
            LOD = (NCONT[K] ^ NCONT[I]) & 307;
            if ( LOD != 290 ) goto L330;
            //   A CENTER IS GENERATED
            if ( NXC+NYC+NZC == 0 ) goto L330;
            if ( L[1][2] == 15 && I == IJ+1 ) goto L324;
L321:
            if ( NXC == 0 ) goto L322;
            if ( ICO[4][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NXC]*4;
            if ( ICO[4][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NXC]*4;
L322:
            if ( NYC == 0 ) goto L323;
            if ( ICO[5][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NYC]*64;
            if ( ICO[5][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NYC]*64;
L323:
            if ( NZC == 0 ) goto L325;
            if ( ICO[6][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NZC]*512;
            if ( ICO[6][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NZC]*512;
            goto L325;
L324:
            L[1][2] = 30;
            NIJ = ICO[6][1];
            NIJ = NDAT[NIJ+4];
            NJX = NIJ+NXC % 4;
            NCONT[IJ] = (NCONT[IJ] & 2035)+NDAT[NJX+4]*4;
            NCONT[IJ] = (NCONT[IJ] & 1855)+NDAT[NIJ+4]*64;
            goto L321;
L325:
            ICO[4][NCI] = 0;
            ICO[4][NCK] = 0;
            ICO[5][NCK] = 0;
            ICO[5][NCI] = 0;
            ICO[6][NCI] = 0;
            ICO[6][NCK] = 0;
L330:
            ICO4 = (NCONT[I]/4) % 4;
            IKO4 = (NCONT[K]/4) % 4;
            ICO5 = (NCONT[I]/64) % 4;
            IKO5 = (NCONT[K]/64) % 4;
            ICO6 = (NCONT[I]/512) % 4;
            IKO6 = (NCONT[K]/512) % 4;
            NI4 = NDAT[ICO4+4];
            NK4 = NDAT[IKO4+4];
            NI5 = NDAT[ICO5+4];
            NK5 = NDAT[IKO5+4];
            NI6 = NDAT[ICO6+4];
            NK6 = NDAT[IKO6+4];
            if ( MJ > J ) goto L390;
            for (M=MJ; M <= J; ++M)
            {
                NCM = M-IJ+1;
                MCO4 = ICO[4][NCM] % 4;
                MCO5 = ICO[5][NCM] % 4;
                MCO6 = ICO[6][NCM] % 4;
                NM4 = NDAT[MCO4+4];
                NM5 = NDAT[MCO5+4];
                NM6 = NDAT[MCO6+4];
                LOD1 = (LOD ^ NCONT[M]) & 307;
                NXC = NI4+NK4+NM4 % 4;
                NYC = NI5+NK5+NM5 % 4;
                NZC = NI6+NK6+NM6 % 4;
                if ( LOD1 != 290 ) goto L350;
                if  ( L[1][2] == 11 ) goto L334;
                if ( NXC+NYC+NZC == 0 ) goto L380;
                if ( IJ == 1 ) goto L331;
                if(NXC +NYC  == 0 )goto L333;
                if ( L[1][3] == 14 ) goto L331;
                if ( L[1][2] == 15 ) goto L331;
                if ( L[1][2] == 14 ) goto L331;
                if ( L[1][2] == 17 ) goto L331;
                if ( L[1][2]+L[1][3]+L[1][4] < 18 && L[1][2] != 9 ) goto L331;
                NXC = (NCONT[1]/2+NXC) % 4;
                NYC = (NCONT[1]/32+NYC) % 4;
                NZC = (NCONT[1]/256+NZC) % 4;
L331:
                if ( NXC == 0 ) goto L332;
                if ( ICO[4][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NXC]*4;
                if ( ICO[4][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NXC]*4;
                if ( ICO[4][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NXC]*4;
L332:
                if ( NYC == 0 ) goto L333;
                if ( ICO[5][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NYC]*64;
                if ( ICO[5][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NYC]*64;
                if ( ICO[5][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NYC]*64;
L333:
                if ( NZC == 0 ) goto L335;
                if ( ICO[6][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NZC]*512;
                if ( ICO[6][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NZC]*512;
                if ( ICO[6][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NZC]*512;
                goto L335;
L334:
                NCONT[I] = (NCONT[I] & 2035);
                NCONT[K] = (NCONT[K] & 1855);
                NCONT[M] = (NCONT[M] & 511);
L335:
                ICO[4][NCI] = 0;
                ICO[5][NCI] = 0;
                ICO[6][NCI] = 0;
                ICO[4][NCK] = 0;
                ICO[5][NCK] = 0;
                ICO[6][NCK] = 0;
                ICO[4][NCM] = 0;
                ICO[5][NCM] = 0;
                ICO[6][NCM] = 0;
                goto L380;
L350:
                if ( LOD1 != 0 ) goto L380;
                //DB 3
                //     PRINT 9,LOD1,NXC,NYC,NZC,NI4,NK4,NI5,NK5,NI6,NK6
                //   9 FORMAT (* LOD1 = *O10* NXC =*I4* NYC =*I4* NZC =*I4/
                //    1 * NI4 =*I3* NK4 =*I3* NI5 =*I3* NK5 =*I3* NI6 =*I3* NK6 =*I3)
                if ( NXC+NYC+NZC == 0 ) goto L359;
                if ( L[1][2] == 30 ) goto L3500;
                if ( L[1][2] == 15 ) goto L355;
                if ( L[1][2] == 17 ) goto L359;
                if ( L[1][2] == 13 ) goto L351;
                if ( L[1][3] == 13 ) goto L352;
                if ( L[1][4] == 13 ) goto L353;
                goto L500;
L3500:
                if ( NXC == 0 ) goto L359;
                if ( L[4][2] == 4 ) goto L359;
                if ( NXC == NK4 ) goto L359;
                NCONT[K] = NCONT[K] ^ (NDAT[NXC]*4);
                if ( L[1][4] == 13 && L[2][4] == 0 ) goto L3580;
                goto L359;
L351:
                NYC = (NK5+NM5) % 4;
                NZC = (NK6+NM6) % 4;
                if ( L[1][3] == 13 ) goto L354;
                if ( L[1][3] == 14 ) goto L354;
                if ( L[1][4] == 13 ) goto L354;
                M2 = I+1;
                //L3511:
                if(L[2][2] == 12 && (L[1][3] == 9  || L[1][4] == 9))  goto L360;
                if ( ICO[5][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NYC]*64;
                if ( ICO[5][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NYC]*64;
                //L3510:
                if ( NZC <= 0 ) goto L360;
                if ( ICO[6][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NZC]*512;
                if ( ICO[6][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NZC]*512;
                goto L360;
L352:
                NXC = (NI4+NM4) % 4;
                NZC = (NI6+NM6) % 4;
                M2 = K+1;
                if(L[2][3] == 12  && (L[1][2] == 9  ||  L[1][4] == 9)) goto L360;
                if ( NXC <= 0 ) goto L3520;
                if ( ICO[4][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NXC]*4;
                if ( ICO[4][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NXC]*4;
L3520:
                if ( NZC <= 0 ) goto L360;
                if ( ICO[6][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NZC]*512;
                if ( ICO[6][NCM] == 4 ) NCONT[M] = NCONT[M]+NDAT[NZC]*512;
                goto L360;
L353:
                NXC = (NI4+NK4) % 4;
                NYC = (NI5+NK5) % 4;
                if(L[2][4] == 12  &&  ( L[1][2] == 9 || L[1][3] == 9)) goto L359;
                if ( NXC <= 0 ) goto L3530;
                if ( ICO[4][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NXC]*4;
                if ( ICO[4][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NXC]*4;
L3530:
                if ( NYC <= 0 ) goto L359;
                if ( ICO[5][NCI] == 4 ) NCONT[I] = NCONT[I]+NDAT[NYC]*64;
                if ( ICO[5][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NYC]*64;
                goto L359;
L354:
                if ( NYC <= 0 ) goto L3540;
                if ( abs(L[2][2]-13) == 1 ) NCONT[I]=NCONT[I]+NDAT[NYC]*64;
L3540:
                if ( NZC <= 0 ) goto L3541;
                if ( ICO[6][NCK] == 4 ) NCONT[K] = NCONT[K]+NDAT[NZC]*512;
L3541:
                if ( NXC <= 0 ) goto L359;
                if ( L[1][4] == 0 || L[1][3] == 14 ) goto L359;
                if ( L[2][4] != 12 ) NCONT[K]=NCONT[K]+NDAT[NXC]*4;
                goto L359;
L355:
                L[1][2] = 30;
                if ( L[1][3] == 14 ) goto L370;
                if ( L[1][3] == 13 && L[2][3] == 12 ) goto L3561;
                if ( L[1][1] ==  7 && L[2][2] == 12 ) goto L357;
                if ( L[1][1] ==  7 && L[2][2] == 14 ) goto L357;
                if ( L[1][1] ==  6 && L[2][2] == 12 ) goto L358;
                if ( L[1][1] ==  6 && L[2][2] == 14 ) goto L358;
                if ( L[1][1] ==  7 && L[1][3] == 13 ) goto L3581;
                if ( (NCONT[I] % 1024) == 19 ) goto L350;
                if ( NXC+NYC > 0 ) goto L356;
                if ( NZC != NM6 ) goto L359;
                if ( NZC <= 0 ) goto L359;
                NCONT[K] = NCONT[K]+NDAT[NZC]*512;
                goto L359;
L356:
                if ( L[3][2] == 10 || L[4][2] == 10 ) goto L359;
                if ( L[1][3] != 10 ) goto L359;
L3561:
                if ( L[2][2] == 3 ) goto L3560;
                NCONT[I] = NCONT[I] ^ 68;
                goto L3580;
L3560:
                if ( L[1][4] == 2 ) NCONT[K]= NCONT[K] ^ 512;
                goto L359;
L357:
                NCONT[I] = NCONT[I]+64;
                if ( L[3][2] != 19 ) NCONT[I+1] = NCONT[I+1]-512;
                if ( L[1][4] == 11 && L[3][2] == 19 )NCONT[I] = NCONT[I]+588;
                goto L3580;
L358:
                NCONT[I] = NCONT[I]+136;
L3580:
                if ( L[1][3] != 13 ) goto L359;
L3581:
                M2 = K+1;
                goto L360;
L359:
                M2 = M+1;
L360:
                if ( M2 > J ) goto L306;
                //DB 2
                //     PRINT 6,M2,I,J,K,M
                //   6 FORMAT (*0M2 =*I3*  I =*I3*  J =*I3* K =*I3*  M =*I3)
                for (M1=M2; M1 <= J; ++M1)
                {
                    NCONT[M1-1] = NCONT[M1];
                    for (M3=1; M3 <= 6; ++M3) ICO[M3][M1-1] = ICO[M3][M1];
                }
                goto L306;
L370:
                if ( L[1][4] > 12 ) goto L371;
                M2 = I+1;
                goto L360;
L371:
                L[1][2] = 13;
                if ( L[2][2] == 12 && L[1][1] == 5 ) NCONT[I] = NCONT[I]+200;
                if ( L[2][2] == 12 && L[1][1] == 6 ) NCONT[I] = NCONT[I]+136;
                if ( L[2][2] == 12 && L[1][1] == 7 ) NCONT[I] = NCONT[I]+652;
                if ( L[2][2] == 13 )                 NCONT[I] = NCONT[I]+68;
                if ( L[2][2] == 14 )                 NCONT[I] = NCONT[I]+140;
                goto L359;
L380:;
            }
L390:;
        }
L400:;
    }
    LD = 0;
    for (I=1; I <= J; ++I) LD = (LD ^ NCONT[I]) & 307;
    if ( LD != 0 ) goto L410;
    NCONT[J-1] = NCONT[J];
    J = J-1;
L410:
    NCONT[J+1] = 0;
    //DB 1
    //     PRINT 4,K,NSPGRP,NAXIS,J,NCONT
L450:
    return;
L500:
    *outputfile << " Did YOU enter the space group correctly?" << endl
        << "  ERRORS of some sort were detected." << endl
        << "  The card was read as:" << SPG << endl;
    NAXIS = 4;
    goto L450;
}

void SPGCOM::GOTOER(void)
{
    *outputfile << "subroutine gotoer was called" << endl;
    throw DBWSException("subroutine gotoer was called");
}



DBWS::DBWS(void)
{
    dc = new DC();
    refls = new REFLS();
    g1 = new G1();
    cntrls = new CNTRLS();
    params = new PARAMS();
    parac = new PARAC();
    jnk = new JNK();
    allp = new ALLP();
    char_ = new CHAR();
    coefc = new COEFC();
    coeff = new COEFF();
    bkgscale = new BKGSCALE();
    datax = new DATAX();
    rtmtx = new RTMTX();
    sizestrain = new SIZESTRAIN();
    convert = new CONVERT();

    spgcom = new SPGCOM();
    spgcom->outputfile = &file6;

    volume = new VOLUME();
    g2 = new G2();
    f1 = new F1();
    g3 = new G3();
    fondi = new FONDI();
    blnk1 = new BLNK1();
    multip = new MULTIP();
    simoper = new SIMOPER();
    hklctl = new HKLCTL();
    cellx = new CELLX();
    prfx = new PRFX();
    pvii = new PVII();
    spvii = new SPVII();
    struphase = new STRUPHASE();
    maxint = new MAXINT();
    codebck = new CODEBCK();
    g4 = new G4();
    comp = new COMP();
    atfat = new ATFAT();
    dircv = new DIRCV();
    labels = new LABELS();
}

DBWS::~DBWS(void)
{
    delete dc;
    delete refls;
    delete g1;
    delete cntrls;
    delete params;
    delete parac;
    delete jnk;
    delete allp;
    delete char_;
    delete coefc;
    delete coeff;
    delete bkgscale;
    delete datax;
    delete rtmtx;
    delete sizestrain;
    delete convert;
    delete spgcom;
    delete volume;
    delete g2;
    delete f1;
    delete g3;
    delete fondi;
    delete blnk1;
    delete multip;
    delete simoper;
    delete hklctl;
    delete cellx;
    delete prfx;
    delete pvii;
    delete spvii;
    delete struphase;
    delete maxint;
    delete codebck;
    delete g4;
    delete comp;
    delete atfat;
    delete dircv;
    delete labels;
}

void DBWS::run(void)
{
    try
    {
        INPTR();
        // Canton et all code starts here !cp may 03 97
        //-----OPEN,if NECESSARY, FILE CONTAINING AMORPHOUS SCATTERING
        if (params->GLB[20] != 0.0 || params->AGLB[20] != 0.0 ) inpam();
        // Canton et all code stops here

        ITER();
        if ( cntrls->MAXS > 0 )
        {
            cntrls->MCYCLX =cntrls->MCYCLE;
            cntrls->MAXSX  = cntrls->MAXS;
            cntrls->MCYCLE = 1;
            cntrls->MAXS   = 0;
            //-------LAST CALL TO ITER if MCYCLE = 1 & MAXS = 0
            ITER();
            cntrls->MCYCLE = cntrls->MCYCLX;
            cntrls->MAXS   = cntrls->MAXSX;
        }
        EXPUT();
    }
    catch (DBWSException& e)
    {
        cout << e.Show() << endl;
    }
}

int  DBWS::sign(int i)
{
    if (i < 0)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

double DBWS::sign(double i)
{
    if (i < 0)
    {
        return -1.0;
    }
    else
    {
        return 1.0;
    }
}

void DBWS::GOTOER(void)
{
    file6 << "subroutine gotoer was called" << endl;
    throw DBWSException("subroutine gotoer was called");
}

void DBWS::SORT(int IPHASE)
{
    int I,J,K,M,IC, LL, IZ, LX, IOF, INP, INP1,IIPHAS;
    int L[IRS+1];
    int ITEMP[IRS+1];
    double TEMP[IRS+1];
    double R;

    IOF=0;
    if (IPHASE >= 2 && IPHASE != 1000)
    {
        for (IIPHAS=2; IIPHAS <= IPHASE; ++IIPHAS) IOF = IOF + refls->ICR[IIPHAS-1];
    }
    if (IPHASE == 1000)
    {
        IC=0;
        for (IIPHAS=1; IIPHAS <= 99; ++IIPHAS) IC  = IC  + refls->ICR[IIPHAS];
    }
    else
    {
        IC=refls->ICR[IPHASE];
    }
    for (I=1; I <= IC; ++I) L[I]=I;
    M=IC-1;

    // TODO: possivel erro aqui!!!!
    //////////////////////////////
    for (LL=1; LL <= 5000; ++LL)
    {
        K=0;
        for (J=1; J <= 2; ++J)
        {
            for (I=J; I <= M; I = I+2)
            {
                INP=L[I]+IOF;
                INP1=L[I+1]+IOF;
                R=refls->REFS[INP1][2]-refls->REFS[INP][2];
                if (R < 0) goto L20; else goto L10;
L20:
                IZ=L[I];
                L[I]=L[I+1];
                L[I+1]=IZ;
                K=K+1;
L10:;
            }
        }
        if (K == 0) goto L5;
    }
    ////////////////////////////////


L5:
    for (I=1; I <= IC; ++I) ITEMP[I]=refls->IREFS[I+IOF];
    for (I=1; I <= IC; ++I)
    {
        LX=L[I];
        refls->IREFS[I+IOF]=ITEMP[LX];
    }
    for (J=1; J <= 3; ++J)
    {
        for (I=1; I <= IC; ++I) TEMP[I]=refls->REFS[I+IOF][J];
        for (I=1; I <= IC; ++I)
        {
            LX=L[I];
            refls->REFS[I+IOF][J]=TEMP[LX];
        }
    }
    for (I=1; I <= IC; ++I) TEMP[I]=refls->FMGNTD[I+IOF];
    for (I=1; I <= IC; ++I)
    {
        LX=L[I];
        refls->FMGNTD[I+IOF]=TEMP[LX];
    }
    if (cntrls->NPROF == _TCHZ)
    {
        for (I=1; I <= IC; ++I) TEMP[I]=refls->HALFL[I+IOF];
        for (I=1; I <= IC; ++I)
        {
            LX=L[I];
            refls->HALFL[I+IOF]=TEMP[LX];
        }
        for (I=1; I <= IC; ++I) TEMP[I]=refls->HALFG[I+IOF];
        for (I=1; I <= IC; ++I)
        {
            LX=L[I];
            refls->HALFG[I+IOF]=TEMP[LX];
        }
        for (I=1; I <= IC; ++I) TEMP[I]=refls->GAM[I+IOF];
        for (I=1; I <= IC; ++I)
        {
            LX=L[I];
            refls->GAM[I+IOF]=TEMP[LX];
        }
    }
}

void DBWS::ASSIGN_(void)
{
    int I,J,K,IN1,IN2,IRC,ICX,IRK,IRL,IRH,MIN,MAX,IPHAS;
    double PX, WT,YUN, RMIN, RMAX;
    int KRR[2+1][IDSZ+1];

    ICX=0;
    for (J=1; J <= cntrls->NPHASE; ++J) ICX = ICX + refls->ICR[J];
    if (cntrls->NPHASE > 1) SORT(1000);
    for (J=1; J <= 2; ++J)
    {
        for (K=1; K <= IDSZ; ++K) KRR[J][K]=0;
    }
    if(cntrls->LST3 == 0) goto L7;
    for (I=1; I <= ICX; ++I)
    {
        IRL=(refls->IREFS[I] % 256)-128;
        IRK=((refls->IREFS[I]/256) % 256)-128;
        IRH=((refls->IREFS[I]/(256*256)) % 256)-128;
        IRC=(refls->IREFS[I]/(256*256*256)) % 8;
        IPHAS=refls->IREFS[I]/(256*256*256*8);
        if ( (I-1 % 60) == 0) file6 << "NO.  CODE    H   K   L  PHASE  HW       POSN" << endl;
        file6 << setw(4) << I
            << setw(4) << IRC << "   "
            << setw(4) << IRH
            << setw(4) << IRK
            << setw(4) << IRL
            << setw(6) << IPHAS
            << setw(8) << setprecision(3) << refls->REFS[I][1]
        << setw(8) << setprecision(3) << refls->REFS[I][2] << endl;
    }
L7:
    for(I=1; I <= ICX; ++I)
    {
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            RMIN=refls->REFS[I][2]-g1->WDT*refls->FWHM[I][1];
            RMAX=refls->REFS[I][2]+g1->WDT*refls->FWHM[I][2];
        }
        else
        {
            RMIN=refls->REFS[I][2]-g1->WDT*refls->REFS[I][1];
            RMAX=refls->REFS[I][2]+g1->WDT*refls->REFS[I][1];
        }
        MIN= static_cast<int>( (RMIN-g1->THMIN)/g1->STEP+1.5 );
        MAX= static_cast<int>( (RMAX-g1->THMIN)/g1->STEP+1.5 );
        MIN=max(MIN,1);
        MIN=min(MIN,datax->NPTS);
        MAX=min(MAX,datax->NPTS);
        for (K=MIN; K <= MAX; ++K)
        {
            KRR[2][K]=I;
            if(KRR[1][K] == 0)KRR[1][K]=I;
        }
    }
    for (J=1; J <= jnk->NEXCRG; ++J)
    {
        if (jnk->AHIGH[J] <= g1->THMIN) goto L482;
        IN1= static_cast<int>( (jnk->ALOW[J]-g1->THMIN)/g1->STEP+1.5 );
        IN1=max(IN1,1);
        IN2= static_cast<int>( (jnk->AHIGH[J]-g1->THMIN)/g1->STEP+1.5 );
        IN2=min(IN2,datax->NPTS);
        for(I=IN1; I <= IN2; ++I)
        {
            KRR[2][I]=1;
            KRR[1][I]=0;
        }
        if(IN2 == datax->NPTS)goto L484;
L482:;
    }
L484:
    if(cntrls->LST2 == 0)goto L530;
    file6 << "PATTERN FROM"
        << setw(8) << setprecision(4) << g1->THMIN
        << " TO" << setw(8) << setprecision(4) << g1->THMAX
        << " IN STEPS OF"  << setw(8) << setprecision(4) << g1->STEP << "DEGREES" << endl;
    file6 << "POSN      I+B     B     I       100*W   K11  K21" << endl;
    for(J=1; J <= datax->NPTS; ++J)
    {
        PX=g1->THMIN + static_cast<double>(J-1) * g1->STEP;
        YUN=datax->Y[J]-datax->BK[J];
        if(cntrls->JOBTYP < 3) WT=1.0/datax->VAR[J];
        file6 << setw(8) << setprecision(4) << PX
            << setw(7) << setprecision(0) << datax->Y[J]
        << setw(7) << setprecision(0) << datax->BK[J]
        << setw(7) << setprecision(0) << YUN
            << setw(9) << setprecision(4) << WT
            << setw(5) << KRR[1][J]
        << setw(5) << KRR[1][J] << endl;
    }
L530:;
    for (I=1; I <= datax->NPTS; ++I)
    {
        if(KRR[2][I]-KRR[1][I] > NOV)
        {
            file7 << "EXCESSIVE PEAK OVERLAP" << endl
                << "     AT THE" << I << "TH STEP THERE ARE "
                << KRR[2][I]-KRR[1][I] << " REFLECTIONS" << endl
                << "     INCREASE THE VALUE OF *NOV* WHICH IS NOW " << NOV << endl;
            file6 << "EXCESSIVE PEAK OVERLAP" << endl
                << "     AT THE" << I << "TH STEP THERE ARE "
                << KRR[2][I]-KRR[1][I] << " REFLECTIONS" << endl
                << "     INCREASE THE VALUE OF *NOV* WHICH IS NOW " << NOV << endl;
            DBWSException("EXCESSIVE PEAK OVERLAP");
        }
    }
    for (I=1; I <= datax->NPTS; ++I) datax->KR[I]=KRR[1][I]+IRS *KRR[2][I];
    for (I=1; I <= datax->NPTS; ++I) if(datax->KR[I] != 0 && datax->KR[I] != IRS )goto L603;
    DBWSException("NO REFLECTIONS FOUND");
    // test for detecting the asymmetry model required    !cp ap 16 97
    // new code included in line 2 of ICF
    // iasym = 0 (usual Rietveld asymmetry)
    // iasym = 1 (new asymmetry. Riello, Canton & Fagherazzi.PD 10,3,204-206,1997)
L603:
    if (cntrls->IASYM == 0)
    {
        // THE FOLLOWING TEST IS REALLY ONLY VALID FOR THE SINGLE PHASE CASE
        if(refls->REFS[ KRR[1][I] ][2] >= g1->RLIM && params->LPAR[1][14] != 0)
        {
            file6 << "ASYMMETRY PARAMETER USAGE INVALID" << endl;
            file7 << "ASYMMETRY PARAMETER USAGE INVALID" << endl;
            DBWSException("ASYMMETRY PARAMETER USAGE INVALID");
        }
    }
    else
    {
        if(refls->REFS[ KRR[1][I] ][2] >= (90.0-g1->RLIM) && params->LPAR[1][14] != 0)
        {
            file6 << "ASYMMETRY PARAMETER USAGE INVALID" << endl;
            file7 << "ASYMMETRY PARAMETER USAGE INVALID" << endl;
            DBWSException("ASYMMETRY PARAMETER USAGE INVALID");
        }
    }
    for (I=1; I <= cntrls->NPHASE; ++I)
    {
        if(params->PAR[I][12] == 0.0 && params->PAR[I][13] == 0.0 && params->LPAR[I][13] != 0)
        {
            file6 << "PREFERRED ORIENTATION USAGE INVALID" << endl;
            file7 << "PREFERRED ORIENTATION USAGE INVALID" << endl;
            DBWSException("PREFERRED ORIENTATION USAGE INVALID");
        }
    }
}

void DBWS::CHISQ(void)
{
    int I,IEXC,NPTHI,NPTLOW;
    double DEL,SDELW,D1NOBK,D2NOBK,S1NOBK,S2NOBK,SDELWP;

    g2->S1=0.0;
    S1NOBK=0.0;
    g2->S2=0.0;
    S2NOBK = 0.0;
    g2->SS2=0.0;
    g2->S3=0.0;
    //S3NOBK = 0.0;
    g2->SS4=0.0;
    g2->D1=0.0;
    g2->D2=0.0;
    g2->D4=0.0;
    SDELW = 1.0E+25;
    SDELWP= 1.0E+25;
    D1NOBK = 0.0;
    D2NOBK = 0.0;
    for (I=1; I <= datax->NPTS; ++I)
    {
        if((jnk->NBCKGD != 0 && datax->KR[I] == 0) || datax->KR[I] == IRS )goto L10;
        if(jnk->NEXCRG > 0)
        {
            for (IEXC=1; IEXC <= jnk->NEXCRG; ++IEXC)
            {
                NPTLOW = static_cast<int>( (jnk->ALOW[IEXC]-g1->THMIN)/g1->STEP + 1.5 );
                NPTHI  = static_cast<int>( (jnk->AHIGH[IEXC]-g1->THMIN)/g1->STEP + 1.5 );
                if (I >= NPTLOW && I <= NPTHI) goto L10;
            }
        }
        if (SDELW < 1.0e+24) SDELWP = SDELW;
        DEL=datax->Y[I]-datax->BK[I]-datax->YC[I];
        //DELW = DEL/sqrt(datax->VAR[I]);
        SDELW = DEL;
        g2->S1 = g2->S1 + abs(DEL);
        S1NOBK = S1NOBK + DEL*(datax->Y[I]-datax->BK[I])/datax->Y[I];                              //numerador para calcular Rp(-bcg)
        g2->S2=g2->S2+DEL*DEL/datax->VAR[I];
        S2NOBK=S2NOBK+DEL*DEL*(datax->Y[I]-datax->BK[I])*(datax->Y[I]-datax->BK[I])/(datax->Y[I]*datax->Y[I])/datax->VAR[I];   //numerador para calcular Rwp(-bcg)
        g2->SS2 = g2->SS2 + DEL*DEL;
        if (SDELWP < 1.0e+24) g2->SS4=g2->SS4+(SDELW-SDELWP)*(SDELW-SDELWP);
L10:;
    }
    for (I=1; I <= datax->NPTS; ++I)
    {
        if((jnk->NBCKGD != 0 && datax->KR[I] == 0) || datax->KR[I] == IRS )goto L15;
        if(jnk->NEXCRG > 0)
        {
            for (IEXC=1; IEXC <= jnk->NEXCRG; ++IEXC)
            {
                NPTLOW = static_cast<int>( (jnk->ALOW[IEXC]-g1->THMIN)/g1->STEP + 1.5 );
                NPTHI  = static_cast<int>( (jnk->AHIGH[IEXC]-g1->THMIN)/g1->STEP + 1.5 );
                if (I >= NPTLOW && I <= NPTHI) goto L15;
            }
        }
        g2->D1=g2->D1+datax->Y[I];
        D1NOBK = D1NOBK+(datax->Y[I]-datax->BK[I]);                                //denominador para calcular Rp(-bcg)
        g2->D2=g2->D2+datax->Y[I]*datax->Y[I]/datax->VAR[I];
        D2NOBK = D2NOBK + (datax->Y[I]-datax->BK[I])*(datax->Y[I]-datax->BK[I])/datax->VAR[I];          //denominador para calcular Rwp(-bcg)
        g2->S3=g2->S3+datax->BK[I]+datax->YC[I];
L15:;
    }
    g2->R1=0.0;
    g2->R2=100.0*g2->S1/g2->D1;
    g2->R2NOBK = 100.0*S1NOBK/D1NOBK;                                //Rp without background 03nov00 (no writable)
    g2->R3=100.0*sqrt(g2->S2/g2->D2);
    g2->R3NOBK = 100.0*sqrt(S2NOBK/D2NOBK);                           //Rwp without background 03nov00 (no writable)
}

void DBWS::COMPTON(int K, double STH, double* CISK)
{
    //-----THIS SUBROUTINE IS USED TO COMPUTE:
    //     CISK = COMPTON INTENSITY SCATTERED BY THE K-TH CRYSTALLINE PHASE
    //            AT THE IPM-TH POINT OF THE X-RAY SPECTRUM.
    //     THE METHOD FOLLOWED IS THAT REPORTED IN :
    //*****A NEW ANALYTIC APPROXIMATION TO ATOMIC INCOHERENT X-RAY SCATTERING
    //     INTENSITIES.
    //     BY VEDENE H. SMITH JR. AJIT J. THAKKAR AND DOUGLAS C. CHAPMAN
    //     ACTA CRYST. (1975). A31, 391-392.
    //-----COMPTON SCATTERING OF THE AMORPHOUS PHASE IS CONTAINED INTO AM(S)
    //     MOREOVER CISK IS MULTIPLIED BY THE FOLLOWING GEOMETRICAL FACTOR:
    //     SEE:
    //     RULAND, ACTA CRYST. 1961, 14, 1180.
    //     ASS1 = CORRECTION FOR ABSORPTION OF COMPTON SCATTERING BY THE SAMPLE
    //     BDF  = BREIT-DIRAC RECOIL FACTOR
    //     ASS2 = CORRECTION FOR ABSORPTION BY AIR CONTAINED BETWEEN THE SAMPLE
    //            AND THE COUNTER
    //     ASS3 = CORRECTION FOR THE CUTTING UP OF COMPTON SCATTERING BY THE
    //            MONOCHROMATOR ( ALSO COMPTON SCATTERING IS DifFRACTING )

    const double HMC = {0.024263935};			// HMC = H/MC

    int I,J,NK,IOF, ICX,IRL;
    double S, S2, S4,BDF,ASS1, ASS2, ASS3, FDEN, FNUM;
    double CSP[2 +1];

    //-----NK = ATOMS IN THE ASSIMETRIC UNIT OF K-TH PHASE
    NK = jnk->NATOM[K];
    //-----IRL = NUMBER OF EQUIVALENT POSITION - ALSO THE IDENTITY
    //           AND if IT IS PRESENT THE SIMMETRY CENTRE
    IRL = rtmtx->MLTPHS[K];

    //-----CALCULATE IOF = ALL ATOMS OF THE K-1 PHASES
    IOF = 0;
    if (K > 1)
    {
        for (I = 2; I <= K; ++I) IOF = IOF + jnk->NATOM[I-1];
    }
    for (ICX = 1; ICX <= 2; ++ICX)
    {
        S  =  STH / g1->LAMDA[ICX];
        S2 =  S * S;
        S4 = S2 * S2;
        CSP[ICX] = 0.0;
        for (I = 1; I <= NK; ++I)
        {
            J = comp->PTC[I+IOF];
            FNUM    =   1.0 + comp->CC[1][J] * S2 + comp->CC[2][J] * S4;
            FDEN    = pow( ( 1.0 + comp->CC[3][J] * S2 + comp->CC[4][J] * S4 ) ,2);
            CSP[ICX]=CSP[ICX] + comp->ZEFF[J] * params->XL[I+IOF][5] * (1.0-(FNUM/FDEN));
        }
        CSP[ICX] = CSP[ICX] * static_cast<double>(IRL);
        //-----UPDATE S
        S  = 2.0 * S;
        S2 =   S * S;
        //-----COMPUTE ASS1
        ASS1 =   1.0 + 0.75 * HMC * g1->LAMDA[ICX] * S2;
        //-----COMPUTE BDF
        BDF  = pow(( 1.0 + 0.50 * HMC * g1->LAMDA[ICX] * S2 ) , 2);
        //-----COMPUTE ASS2 = EXP (-DELTA(MU)*D)
        //      WHERE:
        //      MU       = ABSORPTION COEFFICIENT OF AIR
        //      DELTA(MU)= VARIATION OF MU WITH LAMDA, WAS CALCULATED BY RIELLO
        //                 USING REGRESSION ANALYSIS ON ESTIMATED MU BY ASSUMING
        //                 AIR COMPOSITION 20% O2 AND 80% N2 AT 300�K AND 1 ATM.:
        //                 MU = (E ** 3.40089)*(1.0E-04)*( LAMDA ** 2.79287)
        //      D = 17.3 CM. DISTANCE SPECIMENT-COUNTER OR RADIUS OF THE CAMERA
        ASS2 = exp(-1.5*HMC*17.3*(29.99E-04)* pow(g1->LAMDA[ICX],3.79287)*S2);
        //-----COMPUTE ASS3
        //     FORMULA MUST BE CHANGED FOR OTHER RADIATION AND MONOCHROMATOR
        //-----  ASS3 IS A LORENTZIAN FUNCTION
        ASS3=1/(1+params->GLB[18]* pow(S,params->GLB[19]));
        //----- ASS3 IS A GAUSSIAN FUNCTION
        //       ASS3=EXP(-params->GLB[18]*S2)
        //     ASS3=0.38*EXP(-4.0*S2)+0.62*EXP(-0.15*S2)
        CSP[ICX] = CSP[ICX] * ASS2 * ASS3  / ( ASS1 * BDF );
    }
    //     COMPUTE CISK = COMPTON INTENSITY SCATTERED BY THE K-TH PHASE
    *CISK = params->RATIO[1] * CSP[1] + params->RATIO[2] * CSP[2];
}

void DBWS::DISORDER(int K, double STH, int IDERIV, double* SDK, double* DYC, int FONDO, double DERISO[])
{
    //-----THIS SUBROUTINE COMPUTES SDK = THE SCATTERING DUE TO THE THERMAL
    //     OR LATTICE DISORDER IN THE SAMPLE FOR K-TH PHASE AT THE IPM POINT,
    //     (THEN ALSO IN STEP POINTS THAT DO NOT CONTAIN BRAGG REFLECTIONS),
    //     P.RIELLO, G. FAGHERAZZI, D. CLEMENTE AND P.CANTON IN
    //     J. APPL. CRYST. (1995) 28,115-120

    int I,II, NI, NK, LK,IOF,ICX, IRL;
    double FI;
    double DER[2+1];
    double DIS[2+1];
    double STHL2[2+1];
    double DERDIS[NATS+1][2+1];

    //-----COMPUTE IOF = ATOM NUMBER IN THE K-1 PHASE
    IOF = 0;
    if (K > 1)
    {
        for (I = 2; I <= K; ++I) IOF = IOF + jnk->NATOM[I-1];
    }
    for (I=1; I <= NATS; ++I)
    {
        DERDIS[I][1]=0.0;
        DERDIS[I][2]=0.0;
    }
    //-----IRL = NUMBER OF EQUIVALENT POSITIONS- ALSO THE IDENTITY POSITION
    //           AND THE SIMMETRY CENTRE if PRESENT
    IRL = rtmtx->MLTPHS[K];
    //-----NK = NUMBER OF ATOMS IN THE K-TH PHASE
    NK  = jnk->NATOM[K];
    for (ICX = 1; ICX <= 2; ++ICX)
    {
        STHL2[ICX] = pow(( STH / g1->LAMDA[ICX] ) , 2);
        if(FONDO == 1)
        {
            //-----COMPUTE FI2 = SUM OF SQUARE SCATTERING FACTORS DUE TO ALL ATOMS
            //                   IN THE CELL AT LAMDA(ICX) CORRECTED FOR THE ISOTROPIC
            //                    THERMAL FACTORS.
            atfat->FI2[ICX] = 0.0;
            for (I = 1; I <= NK; ++I)
            {
                NI = params->PTR[I+IOF];
                FI = 0.0;
                coeff->AC[10][NI] = 0.0;
                for (II = 1; II <= 9; II = II + 2) FI = FI + coeff->AC[II][NI]*exp(-coeff->AC[II+1][NI]*STHL2[ICX]);
                FI = FI + coeff->DFP[NI];
                atfat->FI2[ICX] += params->XL[I+IOF][5] *
                    static_cast<double>(IRL)*( 1.0 - exp(-params->XL[I+IOF][4]*2.0*STHL2[ICX]) )* (pow(FI,2)+pow(coeff->DFPP[NI],2));
                //-----NEXT LINES EVALUATE THE DERIVATES
                if(params->LP[I+IOF][4] != 0 && IDERIV == 2)
                {
                    DERDIS[I+IOF][ICX] = params->XL[I+IOF][5] * 2.0 * STHL2[ICX] *
                        exp(-params->XL[I+IOF][4]*2.0*STHL2[ICX])*static_cast<double>(IRL)*(pow(FI,2)+pow(coeff->DFPP[NI],2));
                }
                else
                {
                    DERDIS[I+IOF][ICX]=0.0;
                }
            }
            DIS[ICX] = atfat->FI2[ICX];
        }
        if(FONDO == 2)
        {
            //-----COMPUTE FI2 = SUM OF SQUARE SCATTERING FACTORS DUE TO ALL ATOMS
            //                   IN THE CELL AT LAMDA(ICX) CORRECTED FOR THE OVERALL
            //                    THERMAL FACTORS.
            atfat->FI2[ICX] = 0.0;
            for (I = 1; I <= NK; ++I)
            {
                NI = params->PTR[I+IOF];
                FI = 0.0;
                coeff->AC[10][NI] = 0.0;
                for (II = 1; II <= 9; II = II + 2) FI += coeff->AC[II][NI]*exp(-coeff->AC[II+1][NI]*STHL2[ICX]);
                FI += coeff->DFP[NI];
                atfat->FI2[ICX] += params->XL[I+IOF][5] * (pow(FI,2)+pow(coeff->DFPP[NI],2));
            }
            atfat->FI2[ICX] += static_cast<double>(IRL);
            DIS[ICX] = ( 1.0 - exp(-params->PAR[K][2]*2.0*STHL2[ICX]) ) * atfat->FI2[ICX];
            LK =params->LPAR[K][2];
            if(LK != 0 && IDERIV == 2)
            {
                DER[ICX] = 2.0 * STHL2[ICX] * atfat->FI2[ICX] * exp(-params->PAR[K][2]*2.0*STHL2[ICX]);
            }
            else
            {
                DER[ICX] = 0.0;
            }
        }
    }
    //-----COMPUTE SDK = SCATTERING DISORDER DUE TO THE K-TH PHASE
    *SDK = params->RATIO[1] * DIS[1] + params->RATIO[2] * DIS[2];
    //-----COMPUTE  DERIVATIVE OF YC RESPECT TO ISOTROPIC THERMAL FACTORS
    //                   XL(I+IOF,4) IN THE K-TH PHASE
    if (FONDO == 1)
    {
        for (I=1; I <= NK; ++I)
        {
            if(params->LP[I+IOF][4] != 0 && IDERIV == 2)
            {
                DERISO[I+IOF]=params->RATIO[1] * DERDIS[I+IOF][1] + params->RATIO[2] * DERDIS[I+IOF][2];
            }
            else
            {
                DERISO[I+IOF]=0.0;
            }
        }
    }
    //-----COMPUTE  DERIVATIVE OF YC RESPECT TO OVERALL THERMAL FACTOR
    //                   params->PAR[K][2] IN THE K-TH PHASE
    if(FONDO == 2)
    {
        LK =params->LPAR[K][2];				// TODO: Coloquei talez esteja incorreto, verificar depois de retirar os gotos
        if(LK != 0 && IDERIV == 2)
        {
            *DYC = params->RATIO[1] * DER[1] + params->RATIO[2] * DER[2];
        }
        else
        {
            *DYC = 0.0;
        }
    }
}

void DBWS::SMTRY2(int* IPHASE)
{
    //                             THIS SR GENERATES THE FULL SET OF
    //                           EQUIVALENT HKLS FROM ANY MEMBER OF THE SET.
    //                           IT ALSO DETERMINES THE PHASE SHIFTS RELATIVE
    //                           TO THE INPUT HKL PHASE.

    const int KD[6+1] = { 0,   0,6,3,9,4,8};
    const int KE[3+1] = { 0,   0,4,8};
    const int KF[3+1] = { 0,   0,8,4};

    int I,J,L,M,N,LA,JD,MS,NCO,NCX,K1JD,IXIT;
    double CI;
    int LD[3+1][48+1];
    int NCH[48+1];
    int NCK[48+1];
    int NCL[48+1];
    int IPH[48+1];

    if ( spgcom->NC > 0 ) goto L14;
    if ( hklctl->IHKL[1][1] < 0 )
    {
        goto L11;
    }
    else if ( hklctl->IHKL[1][1] == 0 )
    {
        goto L9;
    }
    else
    {
        goto L14;
    }
L9:
    if ( hklctl->IHKL[2][1] < 0 )
    {
        goto L12;
    }
    else if ( hklctl->IHKL[2][1] == 0 )
    {
        goto L10;
    }
    else
    {
        goto L14;
    }
L10:
    if ( hklctl->IHKL[3][1] < 0 )
    {
        goto L13;
    }
    else
    {
        goto L14;
    }
L11:
    hklctl->IHKL[1][1] = -hklctl->IHKL[1][1];
L12:
    hklctl->IHKL[2][1] = -hklctl->IHKL[2][1];
L13:
    hklctl->IHKL[3][1] = -hklctl->IHKL[3][1];
L14:
    IPH[1] = hklctl->IHKL[3][1]+512*(hklctl->IHKL[2][1]+512*hklctl->IHKL[1][1]);
    CI = 1.0;
    hklctl->ICHKL[*IPHASE] = 1;
    hklctl->AZ[1] = 0.0;
    LD[1][1] = 0;
    LD[2][1] = 0;
    LD[3][1] = 0;
    NCH[1] = 0;
    NCK[1] = 0;
    NCL[1] = 1;
    if ( hklctl->N1HKL[*IPHASE] == 0 ) goto L1000;
    L = 2;
    for (I=1; I <= hklctl->N1HKL[*IPHASE]; ++I)
    {
        CI = CI*2.0;
        if ( hklctl->NC1[I][1][*IPHASE] > 0 ) CI = CI*1.5;
        for (J=1; J <= hklctl->ICHKL[*IPHASE]; ++J)
        {
            NCO = hklctl->NC1[I][1][*IPHASE]+1;
            switch (NCO) {
            case 1:
                goto L20;
                break;
            case 2:
                goto L700;
                break;
            case 3:
                goto L800;
                break;
            case 4:
                goto L15;
                break;
            }
            GOTOER();
L15:
            if( ((hklctl->IHKL[1][1]-hklctl->IHKL[2][1]-hklctl->IHKL[3][1]) % 3) != 0 ) goto L1002;
            goto L900;
L20:
            NCH[L] =  NCH[J] ^ hklctl->NC1[I][2][*IPHASE];
            if ( (hklctl->NC1[I][2][*IPHASE] % 2) != 0 ) NCH[L] = NCK[J] ^ hklctl->NC1[I][2][*IPHASE];
            M = 1+(NCH[L] % 2);
            MS = 1-2*((NCH[L]/2) % 2);
            N = abs(NCL[J]);
            hklctl->IHKL[M][L] = hklctl->IHKL[1][N]*MS;
            M = 1+(hklctl->NC1[I][2][*IPHASE] % 2);
            MS = 1-2*((hklctl->NC1[I][2][*IPHASE]/2) % 2);
            NCX = hklctl->NC1[I][3][*IPHASE]+1;
            LD[1][L] = KD[NCX]+LD[M][J]*MS;
            NCK[L] =  NCK[J] ^ hklctl->NC1[I][4][*IPHASE];
            if ( (hklctl->NC1[I][4][*IPHASE] % 2) != 0 ) NCK[L]= NCH[J] ^ hklctl->NC1[I][4][*IPHASE];
            M = 2-(NCK[L] % 2);
            MS = 1-2*((NCK[L]/2) % 2);
            hklctl->IHKL[M][L] = hklctl->IHKL[2][N]*MS;
            M = 2-(hklctl->NC1[I][4][*IPHASE] % 2);
            MS = 1-2*((hklctl->NC1[I][4][*IPHASE]/2) % 2);
            NCX = hklctl->NC1[I][5][*IPHASE]+1;
            LD[2][L] = KD[NCX]+LD[M][J]*MS;
            MS = (1-2*hklctl->NC1[I][6][*IPHASE]) *  sign(NCL[J]);
            NCL[L] = MS*NCL[N];
            hklctl->IHKL[3][L] = hklctl->IHKL[3][N]*MS;
            //L70:
            NCO = hklctl->NC1[I][7][*IPHASE]+1;
            if ( NCO > 5 ) NCO=6;
            MS = 1-2*hklctl->NC1[I][6][*IPHASE];
            if ( N > 1 && NCK[J] >= 4 && M == 1 ) MS=-MS;
            LD[3][L] = KD[NCO]+LD[3][J]*MS;
            //L80:
            IXIT = 0;
L81:
            IPH[L] = hklctl->IHKL[3][L]+512*(hklctl->IHKL[2][L]+512*hklctl->IHKL[1][L]);
            //L87:
            LA = LD[1][L]*hklctl->IHKL[1][1]+LD[2][L]*hklctl->IHKL[2][1]+LD[3][L]*hklctl->IHKL[3][1];
            hklctl->AZ[L] = static_cast<double>(LA)/12.0;
            if ( spgcom->NC != 0 ) goto L89;
            if ( spgcom->NSPGRP < 12 ) goto L89;
            if ( hklctl->NC1[I][1][*IPHASE] > 0 ) goto L89;
            if ( (hklctl->NC1[I][2][*IPHASE] % 2) == 1 ) goto L89;
            if ( IPH[L] <= 0 ) goto L680;
L89:
            for (M=2; M <= L; ++M)
            {
                if ( IPH[M-1] == IPH[L] ) goto L690;
                if ( spgcom->NC != 0 ) goto L90;
                if ( IPH[M-1] == -IPH[L] ) goto L690;
L90:;
            }
            L = L+1;
L680:
            if ( IXIT-1 < 0 )
            {
                goto L890;
            }
            else if ( IXIT-1 == 0 )
            {
                goto L750;
            }
            else
            {
                goto L850;
            }
L690:
            if ( fmod(abs(hklctl->AZ[L]-hklctl->AZ[M-1]),1.0) != 0.0 ) goto L1002;
            goto L680;
L700:
            IXIT = 2;
            JD = J;
L750:
            IXIT = IXIT-1;
            LD[1][L] = LD[1][JD];
            LD[2][L] = LD[2][JD];
            LD[3][L] = LD[3][JD];
            NCH[L] = 0;
            NCK[L] = 0;
            NCL[L] = L;
            K1JD = hklctl->IHKL[1][JD];
            hklctl->IHKL[1][L] = hklctl->IHKL[2][JD];
            hklctl->IHKL[2][L] = hklctl->IHKL[3][JD];
            hklctl->IHKL[3][L] = K1JD;
            JD = L;
            goto L81;
L800:
            IXIT = 4;
            JD = J;
            NCO = hklctl->NC1[I][6][*IPHASE];
L850:
            IXIT = IXIT-2;
            NCH[L] = 0;
            NCK[L] = 4;
            NCL[L] = L;
            LD[1][L] = 0;
            LD[2][L] = 0;
            hklctl->IHKL[1][L] = -hklctl->IHKL[1][JD]-hklctl->IHKL[2][JD];
            hklctl->IHKL[2][L] = hklctl->IHKL[1][JD];
            hklctl->IHKL[3][L] = hklctl->IHKL[3][JD];
            LD[3][L] = KF[NCO]+LD[3][JD];
            if ( (NCH[J] % 2) != 0 ) LD[3][L]=KE[NCO]+LD[3][JD];
            JD = L;
            goto L81;
L890:;
        }
L900:
        hklctl->ICHKL[*IPHASE] = L-1;
    }
L1000:
    hklctl->IER = 0;
    if ( spgcom->NC != 0 ) goto L1001;
    for (M=2; M <= hklctl->ICHKL[*IPHASE]; ++M)
    {
        if ( IPH[M] > 0 ) goto L1003;
        hklctl->IHKL[1][M] = -hklctl->IHKL[1][M];
        hklctl->IHKL[2][M] = -hklctl->IHKL[2][M];
        hklctl->IHKL[3][M] = -hklctl->IHKL[3][M];
L1003:;
    }
L1001:
    return;
L1002:
    hklctl->IER = 1;
    goto L1001;
}

void DBWS::CALCUL(int NN)
{
    double DPRECORX,X, B1, B2, BB, AH, BH, CH, DH, EH, AV, PI, CV, DV, BV,TR, SS, TT, SR,
        YY,DA1, DA3, DA4, DA5, AH2, BH2,BNI, ARG,PAK, TAV, TLR, SAI,
        SBI, ARG2, FFX, FNN, DER, SRD,TLG, TLL, COSA, SINA,
        SSNN, SNXI, SUMA, SUMB, DHDHG, DHDHL,PAKNN,SINTH, COSTH,TANTH,PREXP,
        SNEXI, SLABDA,TANTHE, EXPARG, PRECOR, PREXPX, DPRECOR, PRECORX,ISITH;
    int I, J, K, N,II, IJ,JJ,NX,NM,IR, IV, NI,KL,IOF, IRL, ICX,KKL,ICENT,
        IIPHAS;
    bool VERT,PAC;

    int HNN[3+1];
    double XI[14+1];
    double DERIV[MSZ+1];
    double SNX[NATS+1];
    //double SINTL[NOV+1];
    double SA[NATS+1];
    double SB[NATS+1];
    double TEMP[NATS+1];
    double SNEX[NATS+1];
    double T[3+1];
    double SUMAX[NATS+1][9+1];
    double SUMBX[NATS+1][9+1];
    double H[3+1];
    double AL[3+1][3+1];
    double SM[3+1][3+1];

    PI = 90.0/atan(1.0);              // 360./3.14159265359
    prfx->IPH = refls->IREFS[NN]/(256*256*256*8);  // 256/256/256/8
    IOF = 0;
    if ( prfx->IPH > 1 )
    {
        for (IIPHAS=2; IIPHAS <= prfx->IPH; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
    }
    IRL = rtmtx->MLTPHS[prfx->IPH];
    N = jnk->NATOM[prfx->IPH];
    ICENT = rtmtx->ICNTPHS[prfx->IPH];
    //-----ZEROIZE THE DERIVATIVES OF THIS REFLECTION W.R.T. TO PARAMETERS
    NX=0;
    for (IIPHAS=1; IIPHAS <= cntrls->NPHASE; ++IIPHAS) NX = NX+jnk->NATOM[IIPHAS];
    for (I=1; I <= MSZ; ++I) DERIV[I]=0.0;
    for (I=1; I <= NX; ++I)
    {
        for (J=1; J <= 9; ++J)
        {
            SUMBX[I][J] = 0.0;
            SUMAX[I][J] = 0.0;
        }
    }
    CV=0.0;
    DV=0.0;
    AV=0.0;
    BV=0.0;
    PAC = params->PAR[prfx->IPH][12] != 0.0  ||  params->LPAR[prfx->IPH][12] != 0  ||
          params->PAR[prfx->IPH][13] != 0.0  ||  params->LPAR[prfx->IPH][13] != 0;
    for (I=1; I <= 3; ++I) AL[I][I]=params->PAR[prfx->IPH][I+5];
    AL[3][2]=params->PAR[prfx->IPH][9];
    AL[2][3]=AL[3][2];
    AL[3][1]=params->PAR[prfx->IPH][10];
    AL[1][3]=AL[3][1];
    AL[2][1]=params->PAR[prfx->IPH][11];
    AL[1][2]=AL[2][1];
    AH=params->PAR[prfx->IPH][3];
    BH=params->PAR[prfx->IPH][4];
    CH=params->PAR[prfx->IPH][5];
    DH=params->PAR[prfx->IPH][20];
    if (cntrls->NPROF == _pseudoVoigt) EH=params->PAR[prfx->IPH][21];
    if (cntrls->NPROF == _TCHZ)
    {
        AH2=params->PAR[prfx->IPH][15];
        BH2=params->PAR[prfx->IPH][16];
    }
    B1=params->PAR[prfx->IPH][12];
    B2=params->PAR[prfx->IPH][13];
    NM=(NN % NOV)+1;
    ICX=(refls->IREFS[NN]/(256*256*256)) % 8;
    SLABDA=g1->LAMDA[ICX]*g1->LAMDA[ICX]/4.0;
    N=jnk->NATOM[prfx->IPH];
    refls->FMGNTD[NN]=0.0;
    PAKNN=0.0;
    PRECOR=1.0;
    TR=0.0;
    HNN[3]=(refls->IREFS[NN] % 256)-128;
    HNN[2]=((refls->IREFS[NN]/256) % 256)-128;
    HNN[1]=((refls->IREFS[NN]/(256*256)) % 256)-128;

    for (I=1; I <= 3; ++I) H[I]=HNN[I];
    //-----CALCULATION OF TEMP.FACTOR,POSITION AND FWHM
    SS=0.0;
    for (I=1; I <= 3; ++I)
    {
        hklctl->IHKL[I][1] = HNN[I];
        for (J=I; J <= 3; ++J) SS = HNN[I]*AL[I][J]*HNN[J]+SS;                         // GSAS DH2
    }
    if ( PAC )
    {
        TT = 0.0;
        for (I=1; I <= 3; ++I)
        {
            for (J=I; J <= 3; ++J) TT = TT + jnk->PREF[prfx->IPH][I]*AL[I][J] * jnk->PREF[prfx->IPH][J];             // GSAS DP2
        }
        SMTRY2(&prfx->IPH);
        PRECOR = 0.0;
        PREXP = 0.0;
        DPRECOR = 0.0;
        for (IJ=1; IJ <= hklctl->ICHKL[prfx->IPH]; ++IJ)
        {
            PAK = 0.0;
            for (I=1; I <= 3; ++I)
            {
                for (J=I; J <= 3; ++J) PAK = jnk->PREF[prfx->IPH][I]*AL[I][J]*static_cast<double>(hklctl->IHKL[J][IJ])+PAK;     // GSAS CA
            }
            PAK = PAK*PAK/(TT*SS);
            PAKNN = pow((PI/2.0),2);
            if (PAK != 0) PAKNN=pow(atan(sqrt(abs((1.0-PAK)/PAK))),2);
            if (cntrls->IPREF == 0)
            {
                PREXPX = exp(B1*PAKNN);
                PRECORX = B2+(1.0-B2)*PREXPX;
                DPRECORX = PAKNN*(1.0-B2)*PREXPX/PRECORX;
            }
            else
            {
                PREXPX = B1*B1*PAK+(1.0-PAK)/B1;
                PRECORX = 1.0/pow(PREXPX,1.5);
                DPRECORX = -1.5*(2.0*B1*PAK-(1.0-PAK)/B1/B1)/ (pow(PREXPX,2.5)*PRECORX);
            }
            PREXP = PREXP+PREXPX;
            PRECOR = PRECOR+PRECORX;
            DPRECOR = DPRECOR+DPRECORX;
        }
        PREXP = PREXP/static_cast<double>(hklctl->ICHKL[prfx->IPH]);
        PRECOR = PRECOR/static_cast<double>(hklctl->ICHKL[prfx->IPH]);
        DPRECOR = DPRECOR/static_cast<double>(hklctl->ICHKL[prfx->IPH]);
    }
    //L13:
    //SINTL[NM] = sqrt(SS);
    SSNN = 0.25*SS;
    TAV = exp(-2.0*params->PAR[prfx->IPH][2]*SSNN)*PRECOR;
    SINTH = SLABDA*SS;
    COSTH = 1.0-SINTH;
    TANTH = sqrt(SINTH/COSTH);
    g4->TANN[NM] = TANTH;
    //   Correction of microabsorption
    if (cntrls->IABSR == 1)
    {
        ISITH=(1.0)/(sqrt(SINTH));
        SR = params->GLB[12]*(1.0-params->GLB[8]*exp(-params->GLB[9])+params->GLB[8]*exp(-params->GLB[9]/sqrt(SINTH)))+(1.0-params->GLB[12])*(1+params->GLB[13]*(asin(sqrt(SINTH)))-1.5707963268);
    }
    else if (cntrls->IABSR == 2)
    {
        ISITH = sqrt(SINTH);
        SR = 1.0-params->GLB[13]*(asin(ISITH)-1.5707963268);
    }
    else if (cntrls->IABSR == 3)
    {
        ISITH=(1.0)/(sqrt(SINTH));
        SR = 1.0-params->GLB[8]*exp(-params->GLB[9])+params->GLB[8]*exp(-params->GLB[9]*ISITH);
    }
    else if (cntrls->IABSR == 4)
    {
        ISITH=(1.0)/(sqrt(SINTH));
        SR = 1.0-params->GLB[8]*params->GLB[9]*(1.0-params->GLB[9])-ISITH*params->GLB[8]*params->GLB[9]*(1.0-params->GLB[9]*ISITH);
    }

    refls->REFS[NN][2]=atan(TANTH)*PI;
    refls->HALFG[NN]=(AH*TANTH*TANTH+BH*TANTH+CH+DH/COSTH+EH/(TANTH*TANTH));
    if (refls->HALFG[NN] > 0.0)
    {
        refls->HALFG[NN] = sqrt(refls->HALFG[NN]);
    }
    else
    {
        cout << "1 = " << HNN[1] << " " << HNN[2] << " " << HNN[3]  << endl;

        file6 << "   SQUARE OF FWHM NEGATIVE AT TWO-THETA=" << setw(8) << setprecision(3) << refls->REFS[NN][2] << " FOR PHASE NO. " << setw(4) << prfx->IPH << endl;
        file7 << "   SQUARE OF FWHM NEGATIVE AT TWO-THETA=" << setw(8) << setprecision(3) << refls->REFS[NN][2] << " FOR PHASE NO. " << setw(4) << prfx->IPH << endl;
        cout << "   SQUARE OF FWHM NEGATIVE AT TWO-THETA=" << setw(8) << setprecision(3) << refls->REFS[NN][2] << " FOR PHASE NO. " << setw(4) << prfx->IPH << endl;
        DBWSException("SQUARE OF FWHM IS NEGATIVE");
    }
    if (cntrls->NPROF == _TCHZ)
    {
        refls->HALFL[NN] = AH2*TANTH+BH2*sqrt(1.0+TANTH*TANTH);
        BB = pow((pow(refls->HALFG[NN],5.0)+2.69269*pow(refls->HALFG[NN],4.0)*refls->HALFL[NN]+2.42843*pow(refls->HALFG[NN],3.0)*pow(refls->HALFL[NN],2.0)+4.47163*pow(refls->HALFG[NN],2.0)*pow(refls->HALFL[NN],3.0) +0.07842*refls->HALFG[NN]*pow(refls->HALFL[NN],4.0) + pow(refls->HALFL[NN],5.0)),0.2);
        TLR = refls->HALFL[NN]/BB;
        refls->GAM[NN] = 1.36603*TLR-0.47719*TLR*TLR+0.11116*pow(TLR,3.0);
    }
    else
    {
        BB = refls->HALFG[NN];
    }
    prfx->TL=BB;
    refls->REFS[NN][1]=BB;
    BB=BB*BB;
    //-----VERT=.TRUE. if ASYMMETRY CORRECTION IS TO BE CALCULATED
    VERT=false;
    if(cntrls->IASYM == 0)
    {
        if(refls->REFS[NN][2] <= g1->RLIM && cntrls->NPROF != _SplitPearsonVII) VERT=true;
    }
    else
    {
        if (abs(refls->REFS[NN][2]-90.0) >= g1->RLIM) VERT=true;
    }
    //-----CALCULATION OF COS(H.X),SIN(H.X) AND TEMP. FACTOR FOR EACH ATOM
    //L3:
    for (I=1; I <= N; ++I)
    {
        SNXI=0.0;
        SAI=0.0;
        SBI=0.0;
        for (J=1; J <= 11; ++J) XI[J]=params->XL[I+IOF][J];
        for (IR=1; IR <= IRL; ++IR)
        {
            for (J=1; J <= 3; ++J)
            {
                IV=rtmtx->IVEC[prfx->IPH][IR] /32768 / static_cast<int>(pow(32,3-J));
                IV=(IV % 32);
                SM[J][1]=IV/9-1;
                SM[J][2]=((IV/3) % 3)-1;
                SM[J][3]=(IV % 3)-1;
                T[J] = static_cast<double>( ((rtmtx->IVEC[prfx->IPH][IR]/
                    static_cast<int>(pow(32,3-J))
                    ) % 32) - 16) / 12.0;
            }
            X=0.0;
            for (II=1; II <= 3; ++II)
            {
                YY=0.0;
                X=T[II]*HNN[II]+X;
                for (J=1; J <= 3; ++J) YY=SM[J][II]*HNN[J]+YY;
                H[II]=YY;
            }
            TR=X;
            ARG=TR;
            for (J=1; J <= 3; ++J) ARG=H[J]*XI[J]+ARG;
            ARG=6.28318530718*ARG;
            ARG2=H[1]*H[1]*XI[6]+H[2]*H[2]*XI[7]+H[3]*H[3]*XI[8]+2.0*H[1]*H[2]*XI[9]+2.0*H[1]*H[3]*XI[10]+2.0*H[2]*H[3]*XI[11];
            EXPARG=exp(-ARG2);
            COSA=cos(ARG)*EXPARG;
            SINA=sin(ARG)*EXPARG;
            SAI=SAI+COSA;
            if(ICENT == 1)SBI=SINA+SBI;
            for (JJ=1; JJ <= 3; ++JJ)
            {
                SUMAX[I][JJ]=SUMAX[I][JJ]+H[JJ]*SINA;
                if(ICENT == 1)SUMBX[I][JJ]=SUMBX[I][JJ]+H[JJ]*COSA;
            }
            SUMAX[I][4]=SUMAX[I][4]+H[1]*H[1]*COSA;
            SUMAX[I][5]=SUMAX[I][5]+H[2]*H[2]*COSA;
            SUMAX[I][6]=SUMAX[I][6]+H[3]*H[3]*COSA;
            SUMAX[I][7]=SUMAX[I][7]+H[1]*H[2]*COSA;
            SUMAX[I][8]=SUMAX[I][8]+H[1]*H[3]*COSA;
            SUMAX[I][9]=SUMAX[I][9]+H[2]*H[3]*COSA;
            if(ICENT == 1)
            {
                SUMBX[I][4]=SUMBX[I][4]+H[1]*H[1]*SINA;
                SUMBX[I][5]=SUMBX[I][5]+H[2]*H[2]*SINA;
                SUMBX[I][6]=SUMBX[I][6]+H[3]*H[3]*SINA;
                SUMBX[I][7]=SUMBX[I][7]+H[1]*H[2]*SINA;
                SUMBX[I][8]=SUMBX[I][8]+H[1]*H[3]*SINA;
                SUMBX[I][9]=SUMBX[I][9]+H[2]*H[3]*SINA;
            }
            //L10:;
        }
        TEMP[I]=exp(-params->XL[I+IOF][4]*SSNN);
        SA[I]=SAI;
        SB[I]=SBI;
        NI=params->PTR[I+IOF];
        BNI=coeff->DFPP[NI];
        FFX=coeff->DFP[NI];
        coeff->AC[10][NI]=0.0;
        for (II=1; II <= 9; II = II + 2) FFX=FFX+coeff->AC[II][NI]*exp(-coeff->AC[II+1][NI]*SSNN);
        SNEXI=FFX*params->XL[I+IOF][5]*TEMP[I];
        SNXI=BNI*params->XL[I+IOF][5]*TEMP[I];
        //-----CALCULATE A AND B OF F
        AV=SNEXI*SAI+AV;
        BV=SNEXI*SBI+BV;
        SNEX[I]=2.0*SNEXI*TAV*params->RATIO[ICX];
        SNX[I]=2.0*SNXI*TAV*params->RATIO[ICX];
        CV=CV+SNXI*SAI;
        DV=DV+SNXI*SBI;
    }
    FNN=params->RATIO[ICX]*(CV*CV+AV*AV+DV*DV+BV*BV)*TAV*SR;

    // PREPARING PHASE and struc fact TO BE PRINTED  !cp june 98
    struphase->TAVIX[NN]=TAV;
    struphase->SRIX[NN]=SR;
    if(AV == 0)AV=1E-6;
    struphase->APHASE[NN]=atan(BV/AV);
    if(AV < 0.0) struphase->APHASE[NN] = struphase->APHASE[NN]+3.1415927359;
    if(BV < 0 && AV == 0) struphase->APHASE[NN] =1.5*3.1415927359;
    if(BV > 0 && AV == 0) struphase->APHASE[NN] =0.5*3.1415927359;
    //L120:
    refls->FMGNTD[NN]=FNN;
    if(cntrls->MAXS == 0) return;

    //-----CALCULATE DERIVATIVES
    for (I=1; I <= N; ++I)
    {
        SNEXI=SNEX[I];
        SNXI=SNX[I];
        SAI=SA[I];
        SBI=SB[I];
        for (J=1; J <= 11; ++J)
        {
            K=params->LP[I+IOF][J];
            if(K == 0) goto L22;
            if(J > 5) goto L221;
            if(J > 3)goto L29;
            SUMA=SUMAX[I][J];
            SUMB=SUMBX[I][J];
            DER=-((AV*SUMA-BV*SUMB)*SNEXI+(CV*SUMA-DV*SUMB)*SNXI)*6.2831853071;
            goto L26;
L29:
            if(J > 4)goto L31;
            DER=-((SAI*AV+SBI*BV)*SNEXI+(SAI*CV+SBI*DV)*SNXI)*SSNN;
            goto L26;
L31:
            DER=((SAI*AV+SBI*BV)*SNEXI+(SAI*CV+SBI*DV)*SNXI)/params->XL[I+IOF][5];
            goto L26;
L221:
            SUMA=SUMAX[I][J-2];
            SUMB=SUMBX[I][J-2];
            DER=-((AV*SUMA+BV*SUMB)*SNEXI+(CV*SUMA+DV*SUMB)*SNXI);
            if(J >= 9) DER = 2.0*DER;
L26:
            DERIV[K] = sign(params->A[I+IOF][J])*DER+DERIV[K];
L22:;
        }
    }
    //-----CALCULATE DERIVATIVES
    //-----Preferred Orientation Derivatives
    K = params->LPAR[prfx->IPH][12];
    if ( K != 0 )
    {
        DERIV[K] = DERIV[K]+FNN*DPRECOR;
        //         print '(3x,i3,3f10.5)',k,fnn,dprecor,deriv(k)         ! ********
    }

    //////////////////////////////////////////////////////////
    // TODO: remover este isso. Aideia é dividir a rotina INPTR em duas e fazer estes teste por lá

    K = params->LPAR[prfx->IPH][13];
    if (cntrls->IPREF == 0)
    {
        if(K != 0) DERIV[K]=DERIV[K]+(1.0-PREXP)*FNN/PRECOR;
    }
    else
    {
        if (K != 0)
        {
            file6 << "G2 IS NOT A REFINABLE PARAMETER FOR IPREF = 1" << endl;
            DBWSException("");
        }
    }
    ////////////////////////////////////////////////////

    //-----Derivatives for microabsorption parameter
    if (cntrls->IABSR == 1)
    {
        K = params->LGLB[13];
        SRD = (1.0-params->GLB[12])*(asin(sqrt(SINTH))-1.5707963268);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
        K = params->LGLB[12];
        SRD = 1.0 -params->GLB[8]*exp(-params->GLB[9])+params->GLB[8]*exp(-params->GLB[9]/sqrt(SINTH))-1.0-params->GLB[13]*(asin(sqrt(SINTH))-1.5707963268);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
        K = params->LGLB[9];
        SRD = params->GLB[8]*params->GLB[12]*(exp(-params->GLB[9])-exp(-params->GLB[9]/sqrt(SINTH))/sqrt(SINTH));
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
        K = params->LGLB[8];
        SRD = -params->GLB[12]*(exp(-params->GLB[9])+exp(-params->GLB[9]/sqrt(SINTH)));
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
    }
    else if (cntrls->IABSR == 2)
    {
        ///////////////////////////////
        // TODO: Eliminar este if
        KKL = params->LGLB[12];
        K  = params->LGLB[9];
        KL = params->LGLB[8];
        if (K != 0 || KL != 0 || KKL != 0)
        {
            file6 << "P AND/OR Q AND/OR R ARE NOT REFINABLE PARAMETERS FOR IABSR=2" << endl;
            cout << "P AND/OR Q AND/OR R ARE NOT REFINABLE PARAMETERS FOR IABSR=2" << endl;
            DBWSException("");
        }
        ///////////////////////////

        K = params->LGLB[13];
        SRD = 1.5707963268 - asin(sqrt(SINTH));
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
    }
    else if (cntrls->IABSR == 3)
    {
        /////////////////////////////////////////////////////////
        // TODO: Eliminar este if!!!
        KKL = params->LGLB[13];
        K = params->LGLB[12];
        if (K != 0 || KKL != 0)
        {
            file6 << "R AND/OR T IS NOT A REFINABLE PARAMETER FOR THE IABSR CHOICE" << endl;
            cout << "R AND/OR T IS NOT A REFINABLE PARAMETER FOR THE IABSR CHOICE" << endl;
            DBWSException("");
        }
        ////////////////////////////////////////////////////////////

        K = params->LGLB[9];
        SRD = params->GLB[8]*exp(-params->GLB[9])-params->GLB[8]*ISITH*exp(-params->GLB[9]*ISITH);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN;
        K = params->LGLB[8];
        SRD = -exp(-params->GLB[9])+exp(-params->GLB[9]*ISITH);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN;
    }
    else if (cntrls->IABSR == 4)
    {
        //////////////////////////////////////////
        // TODO: Eliminar este if!!!!
        KKL = params->LGLB[13];
        K = params->LGLB[12];
        if (K != 0 || KKL != 0)
        {
            file6 << "R AND/OR T IS NOT A REFINABLE PARAMETER FOR THE IABSR CHOICE" << endl;
            cout << "R AND/OR T IS NOT A REFINABLE PARAMETER FOR THE IABSR CHOICE" << endl;
            DBWSException("");
        }
        /////////////////////////////

        K = params->LGLB[9];
        SRD = params->GLB[8]*(2*params->GLB[9]-1)+params->GLB[8]*ISITH*(2*params->GLB[9]*ISITH-1);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
        K = params->LGLB[8];
        SRD = params->GLB[9]*(params->GLB[9]-1)-params->GLB[9]*ISITH*(1-params->GLB[9]*ISITH);
        if (K != 0) DERIV[K] = DERIV[K] + SRD*FNN/SR;
    }
    //----Overall Temperature and Scale Factor
    K=params->LPAR[prfx->IPH][2];
    if(K != 0) DERIV[K]=DERIV[K]-2.0*SSNN*FNN;
    K=params->LPAR[prfx->IPH][1];
    if(K != 0) DERIV[K]=DERIV[K]+FNN/params->PAR[prfx->IPH][1];
    SINTH=FNN*PI*SLABDA/(sqrt(SINTH*COSTH)*BB);
    SS=FNN/prfx->TL;
    X=TANTH*TANTH;
    //-----Broadening Derivatives
    if (cntrls->NPROF == _SplitPearsonVII || cntrls->NPROF == _TCHZ) goto L9212;
    for (J=3; J <= 5; ++J)
    {
        K=params->LPAR[prfx->IPH][J];
        if(K == 0) goto L78;
        DERIV[K]=X*SS+DERIV[K];
L78:
        X=X/TANTH;
    }
    K=params->LPAR[prfx->IPH][21];
    if(K == 0) goto L9212;
    // for cot^2 case                                    !cp nov 29 96
    DERIV[K]=SS/(TANTH*TANTH)+DERIV[K];
    //-----Split Pearson VII Broadening Derivatives
L9212:
    if (cntrls->NPROF == _SplitPearsonVII)
    {
        if (prfx->DELTA < 0.0)
        {
            DA3 = spvii->DA3L;
            DA1 = spvii->DA1L;
        }
        else
        {
            DA3 = spvii->DA3H;
            DA1 = spvii->DA1H;
        }
        for (J=3; J <= 5; ++J)
        {
            K=params->LPAR[prfx->IPH][J];
            if(K == 0) goto L780;
            DERIV[K]=DERIV[K]+X*SS*((DA3*prfx->DELT/(1.0+DA1*prfx->DELT))-(1.0/prfx->TL));
L780:
            X=X/TANTH;
        }
    }
    //L9211:

    //-----TCHZ Broadening Derivatives
    if (cntrls->NPROF == _TCHZ)
    {
        prfx->TL = refls->REFS[NN][1];
        TLG = refls->HALFG[NN];
        TLL = refls->HALFL[NN];
        DHDHG = 0.2/pow(prfx->TL,4.0)*(5.*pow(TLG,4.0)+10.77076*pow(TLG,3.0)*TLL+ 7.28529*TLG*TLG*TLL*TLL+8.94326*TLG*pow(TLL,3.0) + 0.07842*pow(TLL,4.0));
        DHDHL = 0.2/pow(prfx->TL,4.0)*(2.69269*pow(TLG,4.0)+ 4.85686*pow(TLG,3.0)*TLL +13.41489*TLG*TLG*TLL*TLL + 0.31368*TLG*pow(TLL,3.0)+5.*pow(TLL,4.0));
        for (J=3; J <= 5; ++J)
        {
            K=params->LPAR[prfx->IPH][J];
            if(K == 0)goto L9078;
            DERIV[K]=DHDHG*X*SS+DERIV[K];
L9078:
            X=X/TANTH;
        }
        K=params->LPAR[prfx->IPH][20];
        if  (K != 0) DERIV[K]=DHDHG*SS/COSTH+DERIV[K];
        K = params->LPAR[prfx->IPH][15];
        if (K == 0) goto L9213;
        DERIV[K] = 2.0*FNN*DHDHL*TANTH+DERIV[K];
L9213:
        K = params->LPAR[prfx->IPH][16];
        if (K == 0) goto L9214;
        DERIV[K] = 2.0*FNN*DHDHL/sqrt(COSTH) + DERIV[K];
L9214:;
    }
    //-----Profile Shape Derivatives
    K = params->LPAR[prfx->IPH][17];
    if(K != 0 && (cntrls->NPROF == _pseudoVoigt || cntrls->NPROF == _PearsonVII)) DERIV[K]=DERIV[K]+ FNN;
    K = params->LPAR[prfx->IPH][18];
    if(K != 0 && cntrls->NPROF == _pseudoVoigt) DERIV[K]=DERIV[K]+ FNN * refls->REFS[NN][2];
    if(K != 0 && cntrls->NPROF == _PearsonVII) DERIV[K]=DERIV[K]+ FNN / refls->REFS[NN][2];
    K = params->LPAR[prfx->IPH][19];
    if(K != 0 && cntrls->NPROF == _PearsonVII)DERIV[K]=DERIV[K]+FNN/refls->REFS[NN][2] /refls->REFS[NN][2];
    //-----Split Pearson VII Shape Derivative
    if (cntrls->NPROF == _SplitPearsonVII)
    {
        K = params->LPAR[prfx->IPH][17];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1L*prfx->DELT/BB)+ spvii->DA7L*prfx->DELT/BB/(1.0+spvii->DA1L*prfx->DELT/BB));
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6L;
            }
        }
        K = params->LPAR[prfx->IPH][18];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1L*prfx->DELT/BB)+ spvii->DA7L*prfx->DELT/BB/(1.0+spvii->DA1L*prfx->DELT/BB))/refls->REFS[NN][2];
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6L/refls->REFS[NN][2];
            }
        }
        K = params->LPAR[prfx->IPH][19];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1L*prfx->DELT/BB)+spvii->DA7L*prfx->DELT/BB/(1.0+spvii->DA1L*prfx->DELT/BB))/refls->REFS[NN][2]/refls->REFS[NN][2];
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6L/refls->REFS[NN][2]/refls->REFS[NN][2];
            }
        }
        K = params->LPAR[prfx->IPH][24];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6H;
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1H*prfx->DELT/BB)+spvii->DA7H*prfx->DELT/BB/(1.0+spvii->DA1H*prfx->DELT/BB));
            }
        }
        K = params->LPAR[prfx->IPH][25];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6H/refls->REFS[NN][2];
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1H*prfx->DELT/BB)+spvii->DA7H*prfx->DELT/BB/(1.0+spvii->DA1H*prfx->DELT/BB))/refls->REFS[NN][2];
            }
        }
        K = params->LPAR[prfx->IPH][26];
        if (K != 0.0)
        {
            if (prfx->DELTA < 0.0)
            {
                DERIV[K] = DERIV[K]+FNN*spvii->DA6H/refls->REFS[NN][2]/refls->REFS[NN][2];
            }
            else
            {
                DERIV[K] = DERIV[K]+FNN*(-log(1.0+spvii->DA1H*prfx->DELT/BB)+spvii->DA7H*prfx->DELT/BB/(1.0+spvii->DA1H*prfx->DELT/BB))/refls->REFS[NN][2]/refls->REFS[NN][2];
            }
        }
    }
    //-----Split Pearson VII Asymmetry Derivative
    K = params->LPAR[prfx->IPH][27];
    if (prfx->DELTA < 0.0)
    {
        DA1 = spvii->DA1L;
        DA4 = spvii->DA4L;
        DA5 = spvii->DA5L;
    }
    else
    {
        DA1 = spvii->DA1H;
        DA4 = spvii->DA4H;
        DA5 = spvii->DA5H;
    }
    if (K != 0 && cntrls->NPROF == _SplitPearsonVII)
    {
        DERIV[K]=DERIV[K]+FNN*(DA4+DA5*prfx->DELT/BB/(1.0+DA1*prfx->DELT/BB));
    }
    //-----Zero, Displacement, and Transparancy Derivatives
    K=params->LGLB[1];
    if(K != 0)DERIV[K]=DERIV[K]+2.0*FNN/BB;
    K=params->LGLB[10];
    if (K != 0) DERIV[K]=DERIV[K]+2.0*FNN/BB*sqrt(COSTH);
    K=params->LGLB[11];
    if (K != 0) DERIV[K]=DERIV[K]+2.0*FNN/BB*sin(refls->REFS[NN][2]/57.2958);
    //c-----Lattice Parameter Derivatives
    for (J=1; J <= 6; ++J)
    {
        K=params->LPAR[prfx->IPH][J+5];
        if(K == 0) goto L79;
        if(J < 4) X=HNN[J]*HNN[J];
        if(J == 4) X=HNN[2]*HNN[3];
        if(J == 5) X=HNN[1]*HNN[3];
        if(J == 6) X=HNN[1]*HNN[2];
        DERIV[K]= X*SINTH + DERIV[K];
L79:;
    }
    //-Asymmetry Derivative.  Test for asymmetry model included !cp may 97
    K=params->LPAR[prfx->IPH][14];
    if((K != 0) && VERT)
    {
        if (cntrls->IASYM == 0)
        {
            DERIV[K]=-FNN/TANTH+DERIV[K];
        }
        else
        {
            TANTHE=TANTH;
            if (TANTHE >= 1.0) TANTHE=tan(atan(TANTHE-3.14159265359/2));
            DERIV[K]=-FNN/TANTH+DERIV[K];
        }
    }
    //-----STORE DERIVATIVES FOR LIMO REFLECTIONS AT A TIME
    for (I=1; I <= cntrls->MAXS; ++I) g4->DERSTO[NM][I]=DERIV[I];
    //L48:;
}

void DBWS::PRSVII(double T)
{
    //     CALCULATES THE COEFFICIENT C4 AND ITS DERIVATIVE WRT T FOR PHASE K
    //     THIS SUBROUTINE OBTAINED FROM IMMIRZI'S CODE
    //     RGD = GAMMA(T)/GAMMA(T-0.5)
    //     DGD = D(RGD)/D(T)

    const double RGD[] = { 0.0,
        0.156535,  0.183771,  0.209926,  0.235089,  0.259339,
        0.282749,  0.305380,  0.327289, 0.348527,  0.369141,  0.389170,
        0.408654,  0.427625,  0.446115,  0.464153,  0.481764,  0.498972,
        0.515799,  0.532265,  0.548390,  0.564190,  0.579680,  0.594877,
        0.609794,  0.624443,  0.638837,  0.652986,  0.666902,  0.680594,
        0.694071,  0.707342,  0.720415,  0.733297,  0.754996,  0.758519,
        0.770871,  0.783059,  0.795089,  0.806966,  0.818696,  0.830282,
        0.841731,  0.853045,  0.864230,  0.875290,  0.886227,  0.897046,
        0.907750,  0.918344,  0.928828,  0.939207,  0.949484};
    const double DGD[] = { 0.0,
        1.390576,  1.334036,  1.282282,  1.234747,  1.190975,
        1.150537,  1.113079,  1.078304,  1.045950,  1.015831,  0.987556,
        0.961144,  0.936314,  0.913013,  0.891052,  0.870228,  0.850726,
        0.832193,  0.814684,  0.798032,  0.782274,  0.767112,  0.752807,
        0.739098,  0.726022,  0.713542,  0.701621,  0.690110,  0.679232,
        0.668690,  0.658669,  0.648871,  0.639521,  0.630580,  0.621825,
        0.613555,  0.605434,  0.597760,  0.590086,  0.582896,  0.575967,
        0.569075,  0.562482,  0.555962,  0.549890,  0.543743,  0.538044,
        0.532381,  0.526905,  0.521578,  0.516400,  0.511296};
    const double PIG = 3.1415926;
    const double AL2 = 0.6931472;

    int I,N1,IT;
    double DG, RG, FT,FL1;


    if (T < 0.6)
    {
        file6 << endl
            << "UNACCEPTABLE M VALUE IN CALCULATING PEARSON VII FUNCTION "
            << setw(6) << setprecision(2) << T << endl;
        DBWSException("");
    }
    IT=static_cast<int>(T-0.6);
    FT=T-static_cast<double>(IT);
    N1=static_cast<int>((FT-0.6)/0.02+1.0001);
    DG=DGD[N1]+(DGD[N1+1]-DGD[N1])*(FT-0.58-0.02*static_cast<double>(N1))/0.02;
    RG=RGD[N1]+(RGD[N1+1]-RGD[N1])*(FT-0.58-0.02*static_cast<double>(N1))/0.02;
    if (IT > 0)
    {
        for (I=1; I <= IT; ++I)
        {
            FL1=FT+static_cast<double>(I-1);
            DG=DG*(FL1)/(FL1-0.5)-0.5*RG/pow((FL1-0.5),2);
            RG=RG*FL1/(FL1-0.5);
        }
    }
    pvii->TF1=sqrt( pow(2.0,(1.0/T))-1.0);
    pvii->C4=2.0*RG*pvii->TF1/sqrt(PIG);
    pvii->TF2=AL2* pow(2.0,(1.0/T))/(2.0*pow(pvii->TF1,2));
    pvii->TF4=4.0*AL2* pow(2.0,(1.0/T))/T;
    pvii->TF6=4.0*T*pow(pvii->TF1,2);
    pvii->TF8=DG/RG-pvii->TF2/pow(T,2);
    pvii->TF9=4.0*pow(pvii->TF1,2);
}

double DBWS::gamma(double X)
{
    double r;
    int I,IN;
    double GG,GX,DU1, DU2, DU4;


    if (X-1.0 < 0.0) goto L11; else goto L13;
L11:
    if (X-0.1 < 0.0) goto L97; else goto L12;
L12:
    GX=X+1.0;
    goto L21;
L13:
    if (X-2.0 <= 0.0) goto L14; else goto L15;
L14:
    GX=X;
    goto L21;
L15:
    if (X-8.0 <= 0.0) goto L16; else goto L98;
L16:
    for (I=1; I <= 8; ++I)
    {
        IN=I;
        GX=X-static_cast<double>(I);
        if (GX >= 1.0 && GX <= 2.0) goto L21;
    }
L21:
    DU1=GX-1.0;
    DU2=DU1*DU1;
    DU4=DU2*DU2;
    GG=1.0-0.5771917*DU1+0.9882059*DU2-0.8970569*DU1*DU2 +0.9182069*DU4-0.7567041*DU4*DU1+0.4821994*DU4*DU2 -0.1935278*DU1*DU2*DU4+0.0358683*DU4*DU4;
    if (X-1.0 < 0.0) goto L31; else goto L32;
L31:
    r=GG/X;
    goto L99;
L32:
    if (X-2.0 <= 0.0) goto L33; else goto L34;
L33:
    r=GG;
    goto L99;
L34:
    r=X-1.0;
    if (IN-1 <= 0.0) goto L35; else goto L36;
L35:
    r=r*GG;
    goto L99;
L36:
    for (I=2; I <= IN; ++I) r=r*(X-static_cast<double>(I));
    r=r*GG;
    goto L99;
L97:
    cout << "X IN GAMMA(X) IS LESS THAN 0.1   X =" << setw(8) << setprecision(4) << X << endl;
    r=9.513508;
    goto L99;
L98:
    cout << "X IN GAMMA(X) IS GREATER THAN 8.0   X =" << setw(8) << setprecision(4) << X << endl;
    r=5040.0;
L99:
    return r;
}

//    Split Pearson VII coding for function and derivatives
//    Based on Toraya's Code from Profit, Ver 1.22N
void DBWS::mspvii(double A, double W)
{
    double DH, DL, DHD, DLD;

    DL = gamma(spvii->RL-0.5)/(sqrt( pow(2.0,(1.0/spvii->RL))-1.0)*gamma(spvii->RL));
    DH = gamma(spvii->RH-0.5)/(sqrt( pow(2.0,(1.0/spvii->RH))-1.0)*gamma(spvii->RH));
    DLD = gamma(spvii->RL-0.5+0.001)/(sqrt(pow(2.0,(1.0/(spvii->RL+0.001)))-1.0)*gamma(spvii->RL+0.001));
    DHD = gamma(spvii->RH-0.5+0.001)/(sqrt( pow(2.0,(1.0/(spvii->RH+0.001))) -1.0)*gamma(spvii->RH+0.001));
    spvii->DA1L = ( pow(((1.0+A)/A),2.0))*( pow(2.0,(1.0/spvii->RL))-1.0);
    spvii->DA1H = (pow((1.0+A),2.0))*(pow(2.0,(1/spvii->RH))-1.0);
    spvii->DA2L = 1.128379167*(1.0+A)*(1.0/(A*DL+DH))/W;
    spvii->DA2H = spvii->DA2L;
    spvii->DA3L = 2.0*spvii->RL*spvii->DA1L/W;
    spvii->DA3H = 2.0*spvii->RH*spvii->DA1H/W;
    spvii->DA4L = (1.0/(1.0+A))-(DL/(A*DL+DH));
    spvii->DA4H = spvii->DA4L;
    spvii->DA5L = 2.0*spvii->RL*pow(((1+A)/A),2.0)*(1.0+A)/(pow(A,3));
    spvii->DA5H = -2.0*spvii->RH*pow((1+A),2.0)*(1+A);
    spvii->DA6L = -A*1000.0*(DLD-DL)/(A*DL+DH);
    spvii->DA6H = -1000.0*(DHD-DH)/(A*DL+DH);
    spvii->DA7L = log(2.0)*pow(((1.0+A)/A),2.0)*(pow(2.0,(1.0/spvii->RL))-1.0+1.0)/spvii->RL;
    spvii->DA7H = log(2.0)*pow((1.0+A),2.0)*(pow(2.0,(1.0/spvii->RH))-1.0+1.0)/spvii->RH;
}

double DBWS::PROFIL(int N, double X)
{
    double r;

    switch (N) {
    case _Gaussian:
        r=0.939437279*exp(-2.772588722*X);
        prfx->PRFDER=2.772588722;
        break;
    case _Lorentzian:
        r=0.636619772/(1.0+4.0*X);
        prfx->PRFDER=4.0/(1.+4.0*X);
        break;
    case _Mod1:
        r=0.819449653/ pow((1.0+1.656854248*X),2.0);
        prfx->PRFDER=3.313708496/(1.0+1.656854248*X);
        break;
    case _Mod2:
        r=0.766420937/pow((1.0+2.349604208*X),1.5);
        prfx->PRFDER=3.5244063/(1.0+2.349604208*X);
        break;
    case _SplitPearsonVII:
        if (prfx->DELTA < 0.0)
        {
            r = pow((1.0+spvii->DA1L*X),(-spvii->RL)) *spvii->DA2L;
            prfx->PRFDER = spvii->DA1L*spvii->RL/(1.0+spvii->DA1L*X);
        }
        else
        {
            r = pow((1.0+spvii->DA1H*X),(-spvii->RH)) *spvii->DA2H;
            prfx->PRFDER = spvii->DA1H*spvii->RH/(1.0+spvii->DA1H*X);
        }
        break;
    case _pseudoVoigt:
        r=prfx->GAM1*0.636619772/(1.0+4.0*X)+(1-prfx->GAM1)*0.939437279*exp(-2.772588722*X);
        prfx->PRFDER=(prfx->GAM1*2.546479088/ pow((1.0+4.0*X),2) +(1-prfx->GAM1)*2.6046732048*exp(-2.772588722*X))/r;
        break;
    case _PearsonVII:
        r=pvii->C4/pow((1.0+4.0*( pow(2,(1.0/prfx->GAM1)) -1)*X) , prfx->GAM1);
        prfx->PRFDER=pvii->TF6*prfx->GAM1/(1.0+4.0*( pow(2,(1.0/prfx->GAM1))-1)*X);
        break;
    case _TCHZ:
        r=prfx->GAM1*0.636619772/(1.0+4.0*X)+(1-prfx->GAM1)*0.939437279*exp(-2.772588722*X);
        prfx->PRFDER=(prfx->GAM1*2.546479088/ pow((1.0+4.0*X),2) +(1-prfx->GAM1)*2.6046732048*exp(-2.772588722*X))/r;
        break;
    default:
        DBWSException("ILLEGAL PROFILE FUNCTION REQUEST");
    }
    return r;
}

void DBWS::SUMMAT(int IPM, double CSK[], double DISK[], double DYCDD[], double ISODER[], double TOTCS)
{
    int I, J, K, M,II, IL, KK, LK, KM,LK1, LK2,IOF,KRP1, IISO;
    double X, Z, X1, BB,YX,DER, TLL, TLR,ASS5,ESSE,OMEGA, YCALC,
        TANNJ, SHIFT, OMEGA8, LAMDAM,DERMON,PRTEMP;
    bool VERT;
    double DERIV[MSZ+1];

    SHIFT = 0.0;
    for (J=1; J <= MSZ; ++J) DERIV[J]=0.0;
    YCALC=0.0;
    IL=0;
    //-----CALCULATE THE CONTRIBUTION OF THE REFLECTIONS ORD1 TO ORD2 TO THE
    //-----DERIVATIVES W.R.T. THE PROFILE INTENSITY YOBS
    if( g3->IORD1 == 0) goto L12;
    for (I=g3->IORD1; I <= g3->IORD2; ++I)
    {
        prfx->IPH=refls->IREFS[I]/(256*256*256*8);
        IL=IL+1;
        J=(I % NOV)+1;
        // test for asymmetry function !cp ap 12 97
        if (cntrls->IASYM == 0)
        {
            VERT = refls->REFS[I][2] <= g1->RLIM;
        }
        else
        {
            VERT = abs(refls->REFS[I][2]-90.0) >= g1->RLIM;
        }
        SHIFT = params->GLB[10] * cos(refls->REFS[I][2]/2.0/57.2958) + params->GLB[11] * sin(refls->REFS[I][2]/57.2958);
        prfx->DELTA=g3->TH-refls->REFS[I][2]-params->GLB[1]-SHIFT;
        //TANTH=tan(g3->TH*3.14159265359/360.0);
        prfx->DELT=prfx->DELTA*prfx->DELTA;
        prfx->TL=refls->REFS[I][1];
        if (cntrls->NPROF == _pseudoVoigt) prfx->GAM1 = params->PAR[prfx->IPH][17] + params->PAR[prfx->IPH][18] * refls->REFS[I][2];
        if (cntrls->NPROF == _PearsonVII) prfx->GAM1 = params->PAR[prfx->IPH][17] + params->PAR[prfx->IPH][18] / refls->REFS[I][2] + params->PAR[prfx->IPH][19]/refls->REFS[I][2]/refls->REFS[I][2];
        if (cntrls->NPROF == _PearsonVII) PRSVII(prfx->GAM1);
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            spvii->RL=params->PAR[prfx->IPH][17]+(params->PAR[prfx->IPH][18]+params->PAR[prfx->IPH][19]/refls->REFS[I][2])/refls->REFS[I][2];
            spvii->RH=params->PAR[prfx->IPH][24]+(params->PAR[prfx->IPH][25]+params->PAR[prfx->IPH][26]/refls->REFS[I][2])/refls->REFS[I][2];
            mspvii(params->PAR[prfx->IPH][27],prfx->TL);
        }
        if(cntrls->NPROF == _TCHZ)
        {
            TLL = refls->HALFL[I];
            prfx->GAM1 = refls->GAM[I];
            TLR = TLL/prfx->TL;
        }
        BB=prfx->TL*prfx->TL;
        //-----NEXT LINE IS NECESSEARY FOR 2 PHASES WITH VERY DifFERENT FWHM.
        if (prfx->DELT/BB > g1->WDT*g1->WDT) goto L33;
        if (VERT)
        {
            //       test for asymmetry model               !cp may 01 97
            if (cntrls->IASYM == 0)
            {
                YX=prfx->DELT*sign(prfx->DELTA);
                Z=1.0-params->PAR[prfx->IPH][14]*YX/g4->TANN[J];
                if ( Z <= 0.0 ) Z=0.0001;
            }
            else
            {
                YX=sign(prfx->DELTA)*prfx->DELTA/(2*prfx->TL);
                TANNJ=g4->TANN[J];
                if (TANNJ >= 1.0) TANNJ=tan(atan(TANNJ)-3.14159265359/2);
                Z=(params->PAR[prfx->IPH][14]/TANNJ) * (2.0*(prfx->DELTA/(2*prfx->TL))*exp(-YX));
                Z=1+Z;
                if ( Z <= 0.0 ) Z=0.0001;
            }
        }
        else
        {
            Z=1.0;
        }
        //L5:
        PRTEMP = PROFIL(cntrls->NPROF,prfx->DELT/BB);
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            OMEGA = refls->REFS[I][3]*Z*PRTEMP*params->PAR[prfx->IPH][1];
        }
        else
        {
            OMEGA = refls->REFS[I][3]*Z*PRTEMP*params->PAR[prfx->IPH][1]/prfx->TL;
        }
        YCALC = YCALC+OMEGA*refls->FMGNTD[I];
        if ( cntrls->JOBTYP > 2 ) goto L33;
        X = prfx->PRFDER*2.0* prfx->DELT/BB-1.0;
        for (K=1; K <= cntrls->MAXS; ++K)
        {
            DER=1.0;
            //-----Broadening Coeficients Derivatives
            if(cntrls->NPROF != _SplitPearsonVII)
            {
                for (M=3; M <= 5; ++M) if(params->LPAR[prfx->IPH][M] == K) DER=X/prfx->TL/2.0;
            }
            if (params->LPAR[prfx->IPH][20] == K) DER=X/prfx->TL/2.0;
            X1=0.0;
            //-----Asymmetry Derivative
            if (VERT)
            {
                if (cntrls->IASYM == 0)
                {
                    X1=params->PAR[prfx->IPH][14]*sign(prfx->DELTA)*BB/g4->TANN[J]/Z;
                }
                else
                {
                    X1=-params->PAR[prfx->IPH][14]*exp(-YX)*(prfx->TL/(2*prfx->DELTA)-sign(prfx->DELTA)*1.0/4)/TANNJ/Z;
                }
            }
            //-----Zero, Displacement, and Transparancy Derivative
            if ( params->LGLB[1] == K ) DER=prfx->DELTA*(prfx->PRFDER+X1);
            if ( params->LGLB[10] == K ) DER=prfx->DELTA*(prfx->PRFDER+X1);
            if ( params->LGLB[11] == K ) DER=prfx->DELTA*(prfx->PRFDER+X1);
            if ( (params->LPAR[prfx->IPH][14] == K)  &&  VERT )
            {
                if ( cntrls->IASYM == 0 )
                {
                    DER = YX/Z;
                }
                else
                {
                    DER = -2.0*(prfx->DELTA/(2.0*prfx->TL))*exp(-YX)/Z;
                }
            }
            if ( cntrls->NPROF == _TCHZ ) goto L8;
            //-----Pseudo-Voigt Shape Derivatives
            if(cntrls->NPROF == _pseudoVoigt)
            {
                KRP1=params->LPAR[prfx->IPH][17];
                if (K == KRP1) DER=(0.636619772/(1.0+4.0*prfx->DELT/BB)-0.939437279*exp(-2.772588722*prfx->DELT/BB))/PRTEMP;
                KRP1=params->LPAR[prfx->IPH][18];
                if (K == KRP1) DER=(0.636619772/(1.0+4.0*prfx->DELT/BB)-0.939437279*exp(-2.772588722*prfx->DELT/BB))/PRTEMP;
            }
            //-----Pearson VII Shape Derivatives
            if (cntrls->NPROF == _PearsonVII)
            {
                KRP1=params->LPAR[prfx->IPH][17];
                if(K == KRP1) DER=-log(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF4*(prfx->DELT/BB)/(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF8;
                KRP1=params->LPAR[prfx->IPH][18];
                if(K == KRP1)   DER=-log(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF4*(prfx->DELT/BB)/(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF8;
                KRP1=params->LPAR[prfx->IPH][19];
                if(K == KRP1) DER=-log(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF4*(prfx->DELT/BB)/(1.0+pvii->TF9*prfx->DELT/BB)+pvii->TF8;
            }
            //-----Lattice Parameter Derivatives
L8:
            for (M=6; M <= 11; ++M) if(params->LPAR[prfx->IPH][M] == K) DER=(prfx->PRFDER+X1)*prfx->DELTA;
            DERIV[K]=g4->DERSTO[J][K]*DER*OMEGA+DERIV[K];
            //L3:;
        }
        //----TCHZ Profile Derivatives
        if(cntrls->NPROF == _TCHZ)
        {
            OMEGA8 = Z*params->PAR[prfx->IPH][1]*refls->REFS[I][3]/prfx->TL;
            for (K = 1; K <= cntrls->MAXS; ++K)
            {
                for (M=3; M <= 5; ++M)
                {
                    if (params->LPAR[prfx->IPH][M] != K) goto L1001;
                    DERIV[K] = DERIV[K]+ OMEGA8*g4->DERSTO[J][K]/2.0*(0.939437279*exp(-2.772588722*prfx->DELT/BB) - 0.636619772/(1.0+4.0*prfx->DELT/BB)) * (1.36603*TLR/prfx->TL-0.95438*TLR*TLR/prfx->TL+0.33348 * pow(TLR,3.0)/prfx->TL);
L1001:;
                }
                if (params->LPAR[prfx->IPH][20] == K) DERIV[K] = DERIV[K]+ OMEGA8*g4->DERSTO[J][K]/2.0*(0.939437279*exp(-2.772588722*prfx->DELT/BB) - 0.636619772/(1.0+4.0*prfx->DELT/BB)) * (1.36603*TLR/prfx->TL-0.95438*TLR*TLR/prfx->TL+0.33348*pow(TLR,3.0)/prfx->TL);
                if (params->LPAR[prfx->IPH][15] != K) goto L1002;
                DERIV[K] = DERIV[K]+ OMEGA8*(0.939437279*exp(-2.772588722*prfx->DELT/BB) -0.636619772/(1.0+4.0*prfx->DELT/BB)) *((1.36603*TLR/prfx->TL-0.95438*TLR*TLR/prfx->TL+0.33348*pow(TLR,3.0)/prfx->TL)* g4->DERSTO[J][K]/2.0 - refls->FMGNTD[I]*g4->TANN[J]*(1.36603/prfx->TL-0.95438*TLR/prfx->TL+0.33348*TLR*TLR/prfx->TL));
L1002:
                if (params->LPAR[prfx->IPH][16] != K) goto L1003;
                DERIV[K] = DERIV[K]+ OMEGA8*(0.939437279*exp(-2.772588722*prfx->DELT/BB) -0.636619772/(1.0+4.0*prfx->DELT/BB)) *((1.36603*TLR/prfx->TL-0.95438*TLR*TLR/prfx->TL+0.33348*pow(TLR,3.0)/prfx->TL)* g4->DERSTO[J][K]/2.0 - refls->FMGNTD[I]*sqrt(1+g4->TANN[J]*g4->TANN[J])*(1.36603/prfx->TL-0.95438*TLR/prfx->TL+0.33348*TLR*TLR/prfx->TL));
L1003:;
            }
        }
L33:;
    }
    //-----FORM SUMS
L12:
    if(jnk->NBCKGD != 0)goto L11;
    for (II=2; II <= 7; ++II)
    {
        if(params->LGLB[II] == 0)goto L10;
        KM=params->LGLB[II];
        if(II == 2)DERIV[KM]=DERIV[KM]+1.0;
        if(II == 2)goto L10;
        DERIV[KM]=DERIV[KM]+ pow( ((g1->THMIN+static_cast<double>(IPM-1)*g1->STEP)/g1->BKPOS-1.0) , (II-2));
L10:;
    }
L11:
    datax->YC[IPM]=YCALC;
    if (cntrls->JOBTYP > 2) goto L20;
    for (K = 1; K <= cntrls->NPHASE; ++K)
    {
        LK1  = params->LPAR[K][1];
        LK2  = params->LPAR[K][2];
        //
        //-----UPDATING GLOBAL SCALE DERIVATE FOR BKG CONTRIBUTE
        //
        if(cntrls->FONDO == 1 || cntrls->FONDO == 2)
        {
            if(LK1 != 0) DERIV[LK1]=DERIV[LK1]+volume->GCOM[K]*CSK[K]+volume->GCOM[K]*DISK[K];
        }
        //
        //-----UPDATING DERIVATE OF Q OVERALL FOR BKG CONTRIBUTE
        //
        if(cntrls->FONDO == 2)
        {
            if(LK2 != 0) DERIV[LK2] = DERIV[LK2] +  DYCDD[K];
        }
        //
        //-----UPDATING DERIVATE OF ISOTROPIC THERMAL PARAMETERS FOR BKG CONTRIBUTE
        //
        if(cntrls->FONDO == 1)
        {
            IOF = 0;
            if(K > 1)
            {
                for (I = 2; I <= K; ++I) IOF = IOF + jnk->NATOM[I-1];
            }
            for (I = 1; I <= jnk->NATOM[K]; ++I)
            {
                IISO=params->LP[I+IOF][4];
                if(IISO != 0) DERIV[IISO] = DERIV[IISO] + ISODER[I+IOF];
                ISODER[I+IOF]=0.0;
            }
        }
    }
    //-----DYC RESPECT TO AMORPHOUS SCALE FACTOR
    LK = params->LGLB[20];
    if(LK != 0) DERIV[LK] = DERIV[LK] + datax->AMORPHOUS[IPM];
    //-----MONOCHROMATOR PARAMETERS DERIVATIVES
    LAMDAM=(g1->LAMDA[1]*params->RATIO[1]+g1->LAMDA[2]*params->RATIO[2])/(params->RATIO[1]+params->RATIO[2]);
    ESSE=2*sin((g3->TH-params->GLB[1]-SHIFT)*0.00872665)/LAMDAM;
    LK=params->LGLB[18];
    if ( LK != 0 )
    {
        //-------NEXT LINE FOR A LORENTZIAN MONOCHROMATOR BASS-BAND  FUNCTION
        ASS5=1/(1+params->GLB[18]*  pow(ESSE,params->GLB[19]) );
        DERMON=-( pow(ESSE,params->GLB[19]) )/ ( pow((1+params->GLB[18]*  pow(ESSE,params->GLB[19]) ),2) );
        //--------------------------------------------------------------------
        DERIV[LK]=DERIV[LK]+TOTCS/ASS5*DERMON;
    }
    //   !cp ap 20 97
    LK=params->LGLB[19];
    ASS5=1/(1+params->GLB[18]*  pow(ESSE,params->GLB[19]) );
    if ( LK != 0 )
    {
        DERMON=-params->GLB[18]*log(ESSE)*( pow(ESSE,params->GLB[19]) )/ pow( (1+params->GLB[18]*(  pow(ESSE,params->GLB[19]) )) , 2);
        DERIV[LK]=DERIV[LK]+TOTCS/ASS5*DERMON;
    }
    //-----FORM THE UPPER TRIANGULAR OF "RJAC" = MATRIX OF NORMAL EQUATIONS
    prfx->DELTA = datax->Y[IPM]-datax->BK[IPM]-YCALC;
    for (J=1; J <= cntrls->MAXS; ++J)
    {
        X = DERIV[J]/(datax->VAR[IPM]);
        f1->VX[J] = f1->VX[J]+prfx->DELTA*X;
        for (KK=J; KK <= cntrls->MAXS; ++KK) f1->RJAC[J][KK] = f1->RJAC[J][KK]+X*DERIV[KK];
    }
L20:;
}

double DBWS::DPINV(double A1[][MSZ+1], double B1[], int* N)
{
    int I,J,K,I1,MI,MJ,MI1,MK1;
    double r,C,D,S,T;
    double A[MSZ+1][MSZ+1];
    double B[MSZ+1];
    double U[MSZ+1];
    int M[MSZ+1];
    //DIMENSION ,A1(MSZ,MSZ),,B1(MSZ),

    if ( *N != 1 ) goto L50;
    A1[1][1] = 1.0/A1[1][1];
    B1[1] = B1[1]*A1[1][1];
    r = 1;
    goto L99;
L50:
    r = 0;
    for (I=1; I <= *N; ++I)
    {
        B[I]=B1[I];
        for (J=1; J <= *N; ++J) A[I][J]=A1[I][J];
    }
    for (I=1; I <= *N; ++I)
    {
        U[I]=abs(A[I][1]);
        C=U[I];
        for (J=2; J <= *N; ++J)
        {
            U[I]=max(U[I],abs(A[I][J]));
            C=C+abs(A[I][J]);
        }
        r=max(r,C);
        M[I]=I;
        B[I]=B[I]/U[I];
        for (J=1; J <= *N; ++J) A[I][J]=A[I][J]/U[I];
    }
    for (I=1; I <= *N; ++I)
    {
        if(I == *N)goto L15;
        J=I;
        I1=I+1;
        MI=M[I];
        S=abs(A[MI][I]);
        for (K=I1; K <= *N; ++K)
        {
            MI=M[K];
            if(abs(A[MI][I]) <= S)goto L4;
            S=abs(A[MI][I]);
            J=K;
L4:;
        }
        MK1=M[J];
        M[J]=M[I];
        M[I]=MK1;
L15:
        MI1=M[I];
        A[MI1][I]=1./A[MI1][I];
        B[MI1]=B[MI1]*A[MI1][I];
        for (J=1; J <= *N; ++J)
        {
            if(I == J)goto L20;
            A[MI1][J]=A[MI1][J]*A[MI1][I];
L20:;
        }
        for (J=1; J <= *N; ++J)
        {
            if(J == I)goto L30;
            MJ=M[J];
            T=A[MJ][I];
            MI1=M[I];
            B[MJ]=B[MJ]-T*B[MI1];
            A[MJ][I]=-T*A[MI1][I];
            for (K=1; K <= *N; ++K)
            {
                if(K == I)goto L40;
                A[MJ][K]=A[MJ][K]-T*A[MI1][K];
L40:;
            }
L30:;
        }
        //L10:;
    }
    for (I=1; I <= *N; ++I)
    {
        MI1=M[I];
        B1[I]=B[MI1];
        for (J=1; J <= *N; ++J)
        {
            MJ=M[J];
            A1[I][MJ]=A[MI1][J]/U[MJ];
        }
    }
    C=0;
    for (I=1; I <= *N; ++I)
    {
        D=abs(A1[I][1]);
        for (J=2; J <= *N; ++J) D=D+abs(A1[I][J]);
        C=max(C,D);
    }
    r=r*C;
L99:;
    return r;
}

double DBWS::ERROR(double A[][6+1], double B[], double* OMEGA)
{
    double r,X,SUM;
    int I,J;
    //DIMENSION A(6,6),B(6)

    SUM=0.0;
    for (I=1; I <= 6; ++I)
    {
        for (J=I; J <= 6; ++J)
        {
            X=2.0;
            if(I == J) X=1.0;
            SUM=SUM+A[I][J]*B[I]*B[J]*X;
        }
    }
    if (SUM < 0.0) SUM=0.0;
    r=sqrt(SUM * *OMEGA);
    return r;
}

void DBWS::ESD(double SM[][6+1], double V[], double SUM)
{
    int I,IA,IB,IC,ID,IE,IP;
    double DEN,DENL,FNUM,FNUML;
    double S[6+1],R[6+1],E[6+1];
    //DIMENSION SM(6,6),V(6)

    I=0;
    for (IA=1; IA <= 3; ++IA)
    {
        I=I+1;
        IB=IA+1;
        if(IB > 3)IB=IB-3;
        IC=IB+1;
        if(IC > 3)IC=IC-3;
        ID=IA+3;
        IE=IB+3;
        IP=IC+3;
        FNUM=4.0*V[IB]*V[IC]-V[ID]*V[ID];
        DEN=4.0*V[IA]*V[IB]*V[IC]-V[IA]*V[ID]*V[ID]-V[IB]*V[IE]*V[IE]-V[IC]*V[IP]*V[IP]+V[ID]*V[IE]*V[IP];
        S[I]=sqrt(FNUM/DEN);
        R[IA]= pow(-S[I],4) ;
        DENL=1./(DEN*DEN);
        R[IB]=DENL*(4.0*V[IC]*DEN-(4.0*V[IA]*V[IC]-V[IE]*V[IE])*FNUM);
        R[IC]=DENL*(4.0*V[IB]*DEN-(4.0*V[IA]*V[IB]-V[IP]*V[IP])*FNUM);
        R[ID]=-DENL*(2.0*V[ID]*DEN-(2.0*V[IA]*V[ID]-V[IE]*V[IP])*FNUM);
        R[IE]=DENL*FNUM*(2.0*V[IB]*V[IE]-V[ID]*V[IP]);
        R[IP]=DENL*FNUM*(2.0*V[IC]*V[IP]-V[ID]*V[IE]);
        E[I]=ERROR(SM,R,&SUM);
        E[I]=E[I]/(2.0*S[I]);
        FNUM=V[IE]*V[IP]-2.0*V[IA]*V[ID];
        DEN=16.0*V[IA]*V[IA]*V[IB]*V[IC]+V[IE]*V[IE]*V[IP]*V[IP]-4.0*(V[IA]*V[IB]*V[IE]*V[IE]+V[IC]*V[IA]*V[IP]*V[IP]);
        DENL=1.0/(DEN*DEN);
        FNUML=FNUM*FNUM;
        S[I+3]=FNUML/DEN;
        R[IA]=-4.0*FNUM*DENL*(V[ID]*DEN+FNUM*(8.0*V[IA]*V[IB]*V[IC]-V[IB]*V[IE]*V[IE]-V[IC]*V[IP]*V[IP]));
        R[IB]=-4.0*FNUML*DENL*(4.0*V[IA]*V[IA]*V[IC]-V[IA]*V[IE]*V[IE]);
        R[IC]=-4.0*FNUML*DENL*(4.0*V[IA]*V[IA]*V[IB]-V[IA]*V[IP]*V[IP]);
        R[ID]=-4.0*FNUM/DEN*V[IA];
        R[IE]=2.0*FNUM*DENL*(V[IP]*DEN-FNUM*(V[IE]*V[IP]*V[IP]-4.0*V[IA]*V[IB]*V[IE]));
        R[IP]=2.0*FNUM*DENL*(V[IE]*DEN-FNUM*(V[IE]*V[IE]*V[IP]-4.0*V[IC]*V[IA]*V[IP]));
        E[I+3]=ERROR(SM,R,&SUM);
        if(S[I+3] == 0.0) goto L3;
        E[I+3]=E[I+3]/(2.0*sqrt(S[I+3]*(1.0-S[I+3])))*180.0/3.14159265359;
        S[I+3]=atan(sqrt((1.0-S[I+3])/S[I+3]))*180.0/3.14159265359;
        goto L4;
L3:
        S[I+3]=90.0;
        E[I+3]=0.0;
L4:
        if(FNUM < 0.0)S[I+3]=180.0-S[I+3];
        if( SM[ID][ID] == 0.0) E[ID]=0.0;
    }
    if(abs(S[1]-S[2]) < 0.00008) E[1]=E[2];
    for (I=1; I <= 6; ++I)
    {
        SM[I][I]=E[I];
        V[I]=S[I];
    }
}

void DBWS::DIRECT(double SM[][6+1], double V[], int* IPH)
{
    int J,K,L,M;
    double X;

    for (J=1; J <= 6; ++J)
    {
        V[J]=params->PAR[*IPH][J+5];
        K=params->LPAR[*IPH][J+5];
        X=params->APAR[*IPH][J+5];
        if(K == 0)X=0.0;
        for (L=J; L <= 6; ++L)
        {
            M=params->LPAR[*IPH][L+5];
            SM[L][J]=0.0;
            if((M == 0) || (K == 0))goto L1;
            SM[L][J]=f1->RJAC[M][K]*X*params->APAR[*IPH][L+5];
L1:
            SM[J][L]=SM[L][J];
        }
    }
    ESD(SM,V,1.0);
}

void DBWS::OUTSCR(int I, double R2, double R3, double X)
{
    cout << "CYCLE NUMBER - " << I << endl;
    cout << "R-P =" << setw(8) << setprecision(2) << R2 << "%"
        << "    R-WP =" << setw(8) << setprecision(2) << R3 << "%"
        << "       R-EXPECTED =" << setw(8) << setprecision(2) << X << "%"
        << "  S = " << setw(8) << setprecision(2) << R3/X << endl;
}

void DBWS::OUTPTR(int ICYCLE)
{
    int I, J, N, IP, KM,IOF, MMM,ILOC1, ILOC2, MATCH, ICOCO,
        IIPHAS,IINNMOL;
    double RATIODCV,X, V0, SRATIODCV,FT,ARG1, ARG2, ARG3, XFAC,SFIS,VOSQ,
        ARGCOS, WTOTAL;

    double SY[30+1];
    double SZ[30+1];
    double DUMMY[2*MSZ+4  +1];
    double VOL[99+1];
    double DVOL[99+1];
    double W[99+1];
    double DW[99+1];
    double XMASS[99+1];
    double DMASS[99+1];
    double FR[99+1];
    double FRP[99+1];
    double DFR[99+1];
    double STMASSA[99+1];
    double SMASS[99+1];

    for (I=1; I <= 2*MSZ+4; ++I) DUMMY[I] = 0.0;
    file6 << endl
        << "R-P        = " << setw(8) << setprecision(2) << g2->R2 << "%" << endl
        << "R-WP       = " << setw(8) << setprecision(2) << g2->R3 << "%     "
        << "R-WP(Background Removed) = " << setw(8) << setprecision(2) << g2->R3NOBK << "%" << endl;
    file7 << endl
        << "R-P        = " << setw(8) << setprecision(2) << g2->R2 << "%" << endl
        << "R-WP       = " << setw(8) << setprecision(2) << g2->R3 << "%     "
        << "R-WP(Background Removed) = " << setw(8) << setprecision(2) << g2->R3NOBK << "%" << endl;
    if ( cntrls->MAXS == 0  &&  cntrls->MAXSX != 0 )
    {
        X=100.0*sqrt((static_cast<double>(g3->NUM)-static_cast<double>(cntrls->MAXSX))*1.0/g2->D2);
    }
    else
    {
        X=100.0*sqrt((static_cast<double>(g3->NUM)-static_cast<double>(cntrls->MAXS))*1.0/g2->D2);
    }
    if ( cntrls->MAXS == 0  &&  cntrls->MCYCLE == 1 )
    {
        OUTSCR(cntrls->MCYCLX,g2->R2,g2->R3,X);
    }
    else
    {
        OUTSCR(ICYCLE-1,g2->R2,g2->R3,X);
    }
    file6
        << "R-EXPECTED = " << setw(8) << setprecision(2) << X << "%" << endl
        << "S          = " << setw(8) << setprecision(2) << sqrt(g2->S2/static_cast<double>(g3->NUM-cntrls->MAXS))
        << "     SQRT(RESIDUAL/N-P)GOODNESS OF FIT" << endl
        << "D - W D    = " << setw(8) << setprecision(2) << g2->SS4/g2->SS2 << "     UNWEIGHTED DURBIN-WATSON STATISTIC D" << endl;
    I=g3->NUM-cntrls->MAXS;
    file6
        << "N-P        = " << setw(8) << I << endl

        << endl
        << scientific
        << "SUMYDIF     = " << setw(14) << setprecision(8) << g2->S1 << endl
        << "SUMYOBS     = " << setw(14) << setprecision(8) << g2->D1 << endl
        << "SUMYCALC    = " << setw(14) << setprecision(8) << g2->S3 << endl
        << "SUMWYOBSSQ  = " << setw(14) << setprecision(8) << g2->D2 << endl
        << "RESIDUAL    = " << setw(14) << setprecision(8) << g2->S2 << endl
        << "CONDITION   = " << setw(14) << setprecision(8) << g3->COND << endl
        << fixed << endl;

    DUMMY[2*MSZ+1] = g2->R2;
    DUMMY[2*MSZ+2] = g2->R3;
    DUMMY[2*MSZ+3] = sqrt(g2->S2/static_cast<double>(g3->NUM));
    DUMMY[2*MSZ+4] = g2->SS4/g2->SS2;

    //     FINAL PARAMETERS AND R-FACTORS
    if (cntrls->IPLST != 0 && cntrls->MAXS == 0)
    {
        for (IP=1; IP <= cntrls->NPHASE; ++IP)
        {
            IOF=0;
            if (IP > 1)
            {
                for (IIPHAS=2; IIPHAS <= IP; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
            }
            N=jnk->NATOM[IP];
            for (I=1; I <= N; ++I)
            {
                for (J=1; J <= 5; ++J)
                {
                    KM=params->LP[I+IOF][J];
                    if(KM != 0)
                    {
                        //  !cp jun 96 start
                        if (J ==  5)
                        {
                            DUMMY[KM] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                        }
                        else
                        {
                            // !cp jun 96 stop
                            DUMMY[KM] = params->XL[I+IOF][J];
                        }
                        // !cp sept 96 start
                        // !cp sept 96 stop
                    }
                }
            }
            for (I=1; I <= N; ++I)
            {
                for (J=6; J <= 11; ++J)
                {
                    KM=params->LP[I+IOF][J];
                    if(KM != 0)
                    {
                        //  !cp jun 96 start
                        if (J ==  5)
                        {
                            DUMMY[KM] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                        }
                        // !cp jun 96 stop
                        DUMMY[KM]  =params->XL[I+IOF][J];
                    }
                }
            }
            for (J=1; J <= 27; ++J)
            {
                KM=params->LPAR[IP][J];
                if (KM != 0) DUMMY[KM] = params->PAR[IP][J];
            }
            DIRECT(dircv->DCSM,dircv->DCV,&IP);
            for (I=1; I <= 6; ++I)
            {
                KM = params->LPAR[IP][I+5];
                MATCH = 0;
                if (KM != 0)
                {
                    if (I >= 4)
                    {
                        for (MMM=1; MMM <= 3; ++MMM) if (KM == params->LPAR[IP][MMM+5]) MATCH=1;
                    }
                    if (MATCH == 0) DUMMY[KM] = dc->SAVE[IP][I];
                }
            }
        }
        for (J=1; J <= 20; ++J)
        {
            if(cntrls->NPROF == _TCHZ && (J == 14 || J == 15 || J == 16)) goto L6019;
            if(cntrls->NPROF == _PearsonVII && (J == 14 || J == 15 || J == 16)) goto L6019;
            if(cntrls->NPROF == _pseudoVoigt && (J == 14 || J == 15 || J == 16)) goto L6019;
            if(cntrls->NPROF == _SplitPearsonVII && (J == 14 || J == 15 || J == 16)) goto L6019;
            KM=params->LGLB[J];
            if(KM != 0) DUMMY[KM] = params->GLB[J];
L6019:;
        }
        for (I=1; I <= cntrls->MAXSX; ++I) file8o << DUMMY[I];
        for (I=MSZ+1; I <= MSZ+cntrls->MAXSX; ++I) file8o << DUMMY[I];
        for (I=2*MSZ+1; I <= 2*MSZ+4; ++I) file8o << DUMMY[I];
        for (I=1; I <= 4; ++I) if ((I % 2) == 1) allp->ILOC = allp->ILOC + 1;
        //L9487:
        allp->FINAL[allp->ILOC][2-(I % 2)] = DUMMY[2*MSZ+I];
    }
    if (cntrls->MAXS == 0 && cntrls->MCYCLE == 1) return;
    allp->ILOC = 0;
    for (I=1; I <= NFINAL; ++I)
    {
        for (J=1; J <= 2; ++J) allp->FINAL[I][J] = 0.0;
    }
    file7 << "CYCLE NUMBER=" << setw(4) << ICYCLE << endl;
    file6 << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CYCLE NUMBER=" << setw(4) << ICYCLE << endl;
    for (IP=1; IP <= cntrls->NPHASE; ++IP)
    {
        IOF=0;
        if (IP > 1)
        {
            for (IIPHAS=2; IIPHAS <= IP; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }
        file6
            << "PHASE " << setw(2) << IP << ": " << setw(50) << char_->PHSNM[IP] << endl
            << "NEW PARAMETERS, SHIFTS, AND STANDARD DEVIATIONS=" << endl << endl
            << "ATOM     X        DX       SX        Y        DY       SY        Z        DZ       SZ        B      DB     SB      So       DSo      SSo" << endl;
        N=jnk->NATOM[IP];
        multip->TMASSA[IP]=0.0;
        STMASSA[IP]=0.0;
        for (I=1; I <= N; ++I)
        {
            for (J=1; J <= 5; ++J)
            {
                allp->ILOC = allp->ILOC + 1;
                KM=params->LP[I+IOF][J];
                if(KM != 0)goto L6;
                SY[J]=0.0;
                SZ[J]=0.0;
                //  !cp jun 97 start
                if(J == 5)
                {
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                }
                else
                {
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J];
                }
                //  !cp jun 97 stop
                goto L5;
L6:
                SZ[J]=sqrt(abs(f1->RJAC[KM][KM]));
                SY[J]=f1->VX[KM]*params->A[I+IOF][J]*params->RELAX[1];
                //  !cp jun 96 start
                if(J == 5)
                {
                    DUMMY[KM] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    params->XL[I+IOF][J]=params->XL[I+IOF][J]+SY[J];
                    DUMMY[KM+MSZ]  = SY[J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    allp->FINAL[allp->ILOC][2] = SZ[J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                }
                else
                {
                    // !cp jun 96 stop
                    DUMMY[KM]  =params->XL[I+IOF][J];
                    params->XL[I+IOF][J]=params->XL[I+IOF][J]+SY[J];
                    DUMMY[KM+MSZ] = SY[J];
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J];
                    allp->FINAL[allp->ILOC][2] = SZ[J];
                    SMASS[IP]=SZ[5];
                }
L5:;
            }
            // !cp jun 96 start
            //L4:
            file6
                << setw(4) << parac->ATEXT[I+IOF] << "  "
                << setw(9) << setprecision(6) << params->XL[I+IOF][1]
                << setw(9) << setprecision(6) << SY[1]
                << setw(9) << setprecision(6) << SZ[1]
                << setw(9) << setprecision(6) << params->XL[I+IOF][2]
                << setw(9) << setprecision(6) << SY[2]
                << setw(9) << setprecision(6) << SZ[2]
                << setw(9) << setprecision(6) << params->XL[I+IOF][3]
                << setw(9) << setprecision(6) << SY[3]
                << setw(9) << setprecision(6) << SZ[3]
                << setw(9) << setprecision(6) << params->XL[I+IOF][4]
                << setw(9) << setprecision(6) << SY[4]
                << setw(9) << setprecision(6) << SZ[4]
                << setw(9) << setprecision(6) << params->XL[I+IOF][5]*multip->XMLTP[IP]/multip->MURT[I+IOF]
                << setw(9) << setprecision(6) << SY[5]*multip->XMLTP[IP]/multip->MURT[I+IOF]
                << setw(9) << setprecision(6) << SZ[5]*multip->XMLTP[IP]/multip->MURT[I+IOF] << endl;
        }
        file6
            << "ATOM       B11      DB11      SB11       B22      DB22      SB22       B33      DB33      SB33" << endl
            << "           B12      DB12      SB12       B13      DB13      SB13       B23      DB23      SB23" << endl;
        for (I=1; I <= N; ++I)
        {
            for (J=6; J <= 11; ++J)
            {
                allp->ILOC = allp->ILOC + 1;
                KM=params->LP[I+IOF][J];
                if(KM != 0)goto L9;
                SY[J]=0.0;
                SZ[J]=0.0;
                allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J];
                goto L8;
L9:
                SZ[J]=sqrt(abs(f1->RJAC[KM][KM]));
                SY[J]=f1->VX[KM]*params->A[I+IOF][J]*params->RELAX[2];
                //  !cp jun 96 start
                if(J ==  5)
                {
                    DUMMY[KM] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    params->XL[I+IOF][J]=params->XL[I+IOF][J]+SY[J];
                    DUMMY[KM+MSZ] = SY[J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                    allp->FINAL[allp->ILOC][2] = SZ[J]*multip->XMLTP[IP]/multip->MURT[I+IOF];
                }
                else
                {
                    DUMMY[KM]  =params->XL[I+IOF][J];
                    params->XL[I+IOF][J]=params->XL[I+IOF][J]+SY[J];
                    DUMMY[KM+MSZ] = SY[J];
                    allp->FINAL[allp->ILOC][1] = params->XL[I+IOF][J];
                    allp->FINAL[allp->ILOC][2] = SZ[J];
                }
                // !cp jun 96 stop
L8:;
            }
            //L7:
            file6 << setw(4) << parac->ATEXT[I+IOF]
                << setw(10) << setprecision(6) << params->XL[I+IOF][6]
                << setw(10) << setprecision(6) << SY[6]
                << setw(10) << setprecision(6) << SZ[6]
                << setw(10) << setprecision(6) << params->XL[I+IOF][7]
                << setw(10) << setprecision(6) << SY[7]
                << setw(10) << setprecision(6) << SZ[7]
                << setw(10) << setprecision(6) << params->XL[I+IOF][8]
                << setw(10) << setprecision(6) << SY[8]
                << setw(10) << setprecision(6) << SZ[8] << endl
                << "    "
                << setw(10) << setprecision(6) << params->XL[I+IOF][9]
                << setw(10) << setprecision(6) << SY[9]
                << setw(10) << setprecision(6) << SZ[9]
                << setw(10) << setprecision(6) << params->XL[I+IOF][10]
                << setw(10) << setprecision(6) << SY[10]
                << setw(10) << setprecision(6) << SZ[10]
                << setw(10) << setprecision(6) << params->XL[I+IOF][11]
                << setw(10) << setprecision(6) << SY[11]
                << setw(10) << setprecision(6) << SZ[11] << endl;
        }
        for (J=1; J <= 27; ++J)
        {
            if ( J == 12 )
            {
                allp->ILOC = allp->ILOC + 1;
                allp->FINAL[allp->ILOC][1] = jnk->PREF[IP][1];
                allp->FINAL[allp->ILOC][2] = jnk->PREF[IP][2];
                allp->ILOC = allp->ILOC + 1;
                allp->FINAL[allp->ILOC][1] = jnk->PREF[IP][3];
            }
            allp->ILOC = allp->ILOC + 1;
            if ( J == 6 ) ILOC1 = allp->ILOC;
            KM = params->LPAR[IP][J];
            if ( KM == 0 )
            {
                SY[J] = 0.0;
                SZ[J] = 0.0;
                allp->FINAL[allp->ILOC][1] = params->PAR[IP][J];
            }
            else
            {
                SZ[J] = sqrt(abs(f1->RJAC[KM][KM]));
                SY[J] = f1->VX[KM]*params->APAR[IP][J]*params->RELAX[3];
                DUMMY[KM] = params->PAR[IP][J];
                params->PAR[IP][J] = params->PAR[IP][J]+SY[J];
                DUMMY[KM+MSZ] = SY[J];
                allp->FINAL[allp->ILOC][1] = params->PAR[IP][J];
                allp->FINAL[allp->ILOC][2] = SZ[J];
            }
        }
        DIRECT(dircv->DCSM,dircv->DCV,&IP);
        ILOC2 = allp->ILOC;
        allp->ILOC = ILOC1-1;
        for (I=1; I <= 6; ++I)
        {
            allp->ILOC = allp->ILOC + 1;
            KM = params->LPAR[IP][I+5];
            MATCH=0;
            SY[I+5]=dircv->DCV[I]-dc->SAVE[IP][I];
            if (KM != 0)
            {
                if (I >= 4)
                {
                    for (MMM=1; MMM <= 3; ++MMM) if (KM == params->LPAR[IP][MMM+5]) MATCH=1;
                }
                if (MATCH == 0)
                {
                    DUMMY[KM] = dc->SAVE[IP][I];
                    DUMMY[KM+MSZ] = SY[I+5];
                }
            }
            allp->FINAL[allp->ILOC][1] = dircv->DCV[I];
            allp->FINAL[allp->ILOC][2] = dircv->DCSM[I][I];
            dc->SAVE[IP][I]=dircv->DCV[I];
        }
        allp->ILOC = ILOC2;
        if(dircv->DCV[1] == dircv->DCV[2] && dircv->DCV[2] != dircv->DCV[3])
        {
            if(dircv->DCV[4] == dircv->DCV[5] && dircv->DCV[5] != 90.0)
            {
                if(dircv->DCV[6] == 90.0 || dircv->DCV[6] == 120.0)
                {
                    RATIODCV=dircv->DCV[3]/dircv->DCV[1];
                    SRATIODCV=(dircv->DCV[1]*dircv->DCSM[3][3]+dircv->DCV[3]*dircv->DCSM[1][1])/(dircv->DCV[3] * dircv->DCV[3]);
                    file6
                        << "OVERALL SCALE FACTOR=" << scientific
                        << setw(9) << setprecision(3) << params->PAR[IP][1]
                        << setw(9) << setprecision(3) << SY[1]
                        << setw(9) << setprecision(3) << SZ[1] << endl << fixed
                        << "OVERALL TEMP. FACTOR="
                        << setw(9) << setprecision(4) << params->PAR[IP][2]
                        << setw(9) << setprecision(4) << SY[2]
                        << setw(9) << setprecision(4) << SZ[2] << endl
                        << "CELL PARAMETERS="
                        << setw(11) << setprecision(6) << dircv->DCV[1]
                        << setw(11) << setprecision(6) << SY[1+5]
                        << setw(11) << setprecision(6) << dircv->DCSM[1][1] << endl
                        << setw(11) << setprecision(6) << dircv->DCV[2]
                        << setw(11) << setprecision(6) << SY[2+5]
                        << setw(11) << setprecision(6) << dircv->DCSM[2][2] << endl
                        << setw(11) << setprecision(6) << dircv->DCV[3]
                        << setw(11) << setprecision(6) << SY[3+5]
                        << setw(11) << setprecision(6) << dircv->DCSM[3][3] << endl
                        << setw(11) << setprecision(4) << dircv->DCV[4]
                        << setw(11) << setprecision(4) << SY[4+5]
                        << setw(11) << setprecision(4) << dircv->DCSM[4][4] << endl
                        << setw(11) << setprecision(4) << dircv->DCV[5]
                        << setw(11) << setprecision(4) << SY[5+5]
                        << setw(11) << setprecision(4) << dircv->DCSM[5][5] << endl
                        << setw(11) << setprecision(4) << dircv->DCV[6]
                        << setw(11) << setprecision(4) << SY[6+5]
                        << setw(11) << setprecision(4) << dircv->DCSM[6][6] << endl << endl
                        << "            c/a= "  << setw(11) << setprecision(6) << RATIODCV << "  +/-" << setw(11) << setprecision(6) << SRATIODCV << endl << endl
                        << "PREFERRED ORIENTATION PARAMETERS="
                        << " "
                        << setw(8) << setprecision(5) << params->PAR[IP][12]
                        << setw(8) << setprecision(5) << SY[12]
                        << setw(8) << setprecision(5) << SZ[12]
                        << " "
                        << setw(8) << setprecision(5) << params->PAR[IP][13]
                        << setw(8) << setprecision(5) << SY[13]
                        << setw(8) << setprecision(5) << SZ[13] << endl
                        << "ASYMMETRY PARAMETER="
                        << " "
                        << setw(8) << setprecision(4) << params->PAR[IP][14]
                        << setw(8) << setprecision(4) << SY[14]
                        << setw(8) << setprecision(4) << SZ[14] << endl
                        << "LORENTZIAN HALF WIDTH PARAMS (X AND Y) "
                        << " "
                        << setw(8) << setprecision(5) << params->PAR[IP][15]
                        << setw(8) << setprecision(5) << SY[15]
                        << setw(8) << setprecision(5) << SZ[15] << endl
                        << " "
                        << setw(8) << setprecision(5) << params->PAR[IP][16]
                        << setw(8) << setprecision(5) << SY[16]
                        << setw(8) << setprecision(5) << SZ[16] << endl;
                    goto L1032;
                }
            }
        }
        file6 << "OVERALL SCALE FACTOR=" << scientific
            << setw(9) << setprecision(3) << params->PAR[IP][1]
        << setw(9) << setprecision(3) << SY[1]
        << setw(9) << setprecision(3) << SZ[1] << fixed << endl
            << "OVERALL TEMP. FACTOR="
            << setw(9) << setprecision(4) << params->PAR[IP][2]
        << setw(9) << setprecision(4) << SY[2]
        << setw(9) << setprecision(4) << SZ[2] << endl
            << "CELL PARAMETERS=" << endl
            << setw(11) << setprecision(6) << dircv->DCV[1]
        << setw(11) << setprecision(6) << SY[1+5]
        << setw(11) << setprecision(6) << dircv->DCSM[1][1] << endl
            << setw(11) << setprecision(6) << dircv->DCV[2]
        << setw(11) << setprecision(6) << SY[2+5]
        << setw(11) << setprecision(6) << dircv->DCSM[2][2] << endl
            << setw(11) << setprecision(6) << dircv->DCV[3]
        << setw(11) << setprecision(6) << SY[3+5]
        << setw(11) << setprecision(6) << dircv->DCSM[3][3] << endl
            << setw(11) << setprecision(4) << dircv->DCV[4]
        << setw(11) << setprecision(4) << SY[4+5]
        << setw(11) << setprecision(4) << dircv->DCSM[4][4] << endl
            << setw(11) << setprecision(4) << dircv->DCV[5]
        << setw(11) << setprecision(4) << SY[5+5]
        << setw(11) << setprecision(4) << dircv->DCSM[5][5] << endl
            << setw(11) << setprecision(4) << dircv->DCV[6]
        << setw(11) << setprecision(4) << SY[6+5]
        << setw(11) << setprecision(4) << dircv->DCSM[6][6] << endl
            << "PREFERRED ORIENTATION PARAMETERS=" << endl
            << " "
            << setw(8) << setprecision(5) << dircv->DCV[1]
        << setw(8) << setprecision(5) << SY[1+5]
        << setw(8) << setprecision(5) << dircv->DCSM[1][1] << endl
            << " "
            << setw(8) << setprecision(5) << dircv->DCV[2]
        << setw(8) << setprecision(5) << SY[2+5]
        << setw(8) << setprecision(5) << dircv->DCSM[2][2] << endl
            << " "
            << setw(8) << setprecision(5) << dircv->DCV[3]
        << setw(8) << setprecision(5) << SY[3+5]
        << setw(8) << setprecision(5) << dircv->DCSM[3][3] << endl
            << "ASYMMETRY PARAMETER=" << endl
            << " "
            << setw(8) << setprecision(4) << dircv->DCV[4]
        << setw(8) << setprecision(4) << SY[4+5]
        << setw(8) << setprecision(4) << dircv->DCSM[4][4] << endl
            << "LORENTZIAN HALF WIDTH PARAMS (X AND Y)" << endl
            << " "
            << setw(8) << setprecision(5) << dircv->DCV[5]
        << setw(8) << setprecision(5) << SY[5+5]
        << setw(8) << setprecision(5) << dircv->DCSM[5][5] << endl
            << " "
            << setw(8) << setprecision(5) << dircv->DCV[6]
        << setw(8) << setprecision(5) << SY[6+5]
        << setw(8) << setprecision(5) << dircv->DCSM[6][6] << endl
            << endl
            << " "
            << setw(8) << setprecision(5) << params->PAR[IP][12]
        << setw(8) << setprecision(5) << SY[12]
        << setw(8) << setprecision(5) << SZ[12] << endl
            << " "
            << setw(8) << setprecision(5) << params->PAR[IP][13]
        << setw(8) << setprecision(5) << SY[13]
        << setw(8) << setprecision(5) << SZ[13] << endl
            << " "
            << setw(8) << setprecision(5) << params->PAR[IP][14]
        << setw(8) << setprecision(5) << SY[14]
        << setw(8) << setprecision(5) << SZ[14] << endl
            << " "
            << setw(8) << setprecision(5) << params->PAR[IP][15]
        << setw(8) << setprecision(5) << SY[15]
        << setw(8) << setprecision(5) << SZ[15] << endl
            << " "
            << setw(8) << setprecision(5) << params->PAR[IP][16]
        << setw(8) << setprecision(5) << SY[16]
        << setw(8) << setprecision(5) << SZ[16] << endl;
L1032:
        //-----CHECK FOR THE SPLIT PEARSON VII PROFILE
        if(cntrls->NPROF == _SplitPearsonVII)
        {
            file6 << "LOW SIDE EXPONENT PARAMETERS (NA, NB, NC)=" << endl
                //<< scientific
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][17]
                << setw(10) << setprecision(4) << SY[17]
                << setw(10) << setprecision(4) << SZ[17] << endl
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][18]
                << setw(10) << setprecision(4) << SY[18]
                << setw(10) << setprecision(4) << SZ[18] << endl
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][19]
                << setw(10) << setprecision(4) << SY[19]
                << setw(10) << setprecision(4) << SZ[19] << endl
                << "HIGH SIDE EXPONENT PARAMETERS (NA, NB, NC)=" << endl
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][24]
                << setw(10) << setprecision(4) << SY[24]
                << setw(10) << setprecision(4) << SZ[24] << endl
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][25]
                << setw(10) << setprecision(4) << SY[25]
                << setw(10) << setprecision(4) << SZ[25] << endl
                << " "
                << setw(10) << setprecision(4) << params->PAR[IP][26]
                << setw(10) << setprecision(4) << SY[26]
                << setw(10) << setprecision(4) << SZ[26] << endl
                << fixed
                << "SPLIT PEARSON VII ASSYMETRY PARAMETER="
                << " "
                << setw(10) << setprecision(6) << params->PAR[IP][27]
                << setw(10) << setprecision(6) << SY[27]
                << setw(10) << setprecision(6) << SZ[27] << endl;
        }
        else
        {
            //-----if NOT A SPLIT PEARSON VII PROFILE
            file6 << "MIXING PARAMETERS" << endl
                << "NA= "
                //<< scientific
                << setw(10) << setprecision(3) << params->PAR[IP][17]
                << setw(10) << setprecision(3) << SY[17]
                << setw(10) << setprecision(3) << SZ[17] << endl
                << "NB= "
                << setw(10) << setprecision(3) << params->PAR[IP][18]
                << setw(10) << setprecision(3) << SY[18]
                << setw(10) << setprecision(3) << SZ[18] << endl
                << "NC= "
                << setw(10) << setprecision(3) << params->PAR[IP][19]
                << setw(10) << setprecision(3) << SY[19]
                << setw(10) << setprecision(3) << SZ[19] << fixed << endl;
        }
        file6 << "FWHM PARAMETERS=" << endl
            << "U = "
            << setw(10) << setprecision(6) << params->PAR[IP][3]
            << setw(10) << setprecision(6) << SY[3]
            << setw(10) << setprecision(6) << SZ[3] << endl
            << "V = "
            << setw(10) << setprecision(6) << params->PAR[IP][4]
            << setw(10) << setprecision(6) << SY[4]
            << setw(10) << setprecision(6) << SZ[4] << endl
            << "W = "
            << setw(10) << setprecision(6) << params->PAR[IP][5]
            << setw(10) << setprecision(6) << SY[5]
            << setw(10) << setprecision(6) << SZ[5] << endl
            << "CT= "
            << setw(10) << setprecision(6) << params->PAR[IP][21]
            << setw(10) << setprecision(6) << SY[21]
            << setw(10) << setprecision(6) << SZ[21] << endl
            << "Z = "
            << setw(10) << setprecision(6) << params->PAR[IP][20]
            << setw(10) << setprecision(6) << SY[20]
            << setw(10) << setprecision(6) << SZ[20] << endl;

        //-----Modification introduced by Carlos O. Paiva-Santos to perform
        //-----Quantitative phase analysis, 03/94. Added 05/94, T.S. Moss
        //-----CHANGES TO INCORPORATE THE REFINED OCCUPANCY. Paiva-Santos (Feb-Mar/95)
        for (I=1; I <= N; ++I)
        {
            ICOCO=params->PTR[I+IOF];
            multip->TMASSA[IP] = multip->TMASSA[IP] + params->XL[I+IOF][5]*coeff->XMAS[ICOCO]*multip->XMLTP[IP];
            STMASSA[IP] = STMASSA[IP] + SMASS[IP]*coeff->XMAS[ICOCO]*multip->XMLTP[IP];
        }
        XFAC = 3.141592654 / 180.000000;
        dircv->DCV[4] = XFAC * dircv->DCV[4];
        dircv->DCSM[4][4] = dircv->DCSM[4][4] * XFAC;
        dircv->DCV[5] = XFAC * dircv->DCV[5];
        dircv->DCSM[5][5] = dircv->DCSM[5][5] * XFAC;
        dircv->DCV[6] = dircv->DCV[6] * XFAC;
        dircv->DCSM[6][6] = dircv->DCSM[6][6] * XFAC;
        //-----Calculations of VOLUME and SVZM (=W) for each phase
        //-----and respectives standard deviations
        //-----New standard deviation code introduced in nov 96 !cp
        ARGCOS= 1-pow((cos(dircv->DCV[4])),2)-pow((cos(dircv->DCV[5])),2)-pow((cos(dircv->DCV[6])),2) + 2 * (cos(dircv->DCV[4])) * (cos(dircv->DCV[5])) * (cos(dircv->DCV[6]));
        V0 = dircv->DCV[1] * dircv->DCV[2] * dircv->DCV[3];
        VOL[IP] = V0 * sqrt(ARGCOS);
        VOSQ = 0.5*VOL[IP]/ARGCOS;
        ARG1 = VOSQ*(2 * cos(dircv->DCV[4]) * sin(dircv->DCV[4]) - 2*sin(dircv->DCV[4]) *cos(dircv->DCV[5]) *cos(dircv->DCV[6])) * dircv->DCSM[4][4];
        ARG2 = VOSQ*(2 * cos(dircv->DCV[5]) * sin(dircv->DCV[5]) - 2*sin(dircv->DCV[5]) *cos(dircv->DCV[4]) *cos(dircv->DCV[6])) * dircv->DCSM[5][5];
        ARG3 = VOSQ*(2 * cos(dircv->DCV[6]) * sin(dircv->DCV[6]) - 2*sin(dircv->DCV[6]) *cos(dircv->DCV[4]) *cos(dircv->DCV[5])) * dircv->DCSM[6][6];
        DVOL[IP] = sqrt(pow((VOL[IP] * dircv->DCSM[1][1] / dircv->DCV[1]),2) + pow((VOL[IP] * dircv->DCSM[2][2] / dircv->DCV[2]),2) + pow((VOL[IP] * dircv->DCSM[3][3] / dircv->DCV[3]),2) + pow(ARG1,2) + pow(ARG2,2) + pow(ARG3,2));
        // standard deviations are calculed below                      !cp nov 96
        W[IP] = params->PAR[IP][1] * multip->TMASSA[IP] * VOL[IP]/multip->SAQF[IP];
        DW[IP] = (SZ[1]/params->PAR[IP][1]) + (DVOL[IP]/VOL[IP]) + (STMASSA[IP]/multip->TMASSA[IP])/multip->SAQF[IP];
        //   end of std
        file6 << "Volume= "
            << setw(9) << setprecision(3) << VOL[IP]
        << "(+/-)"
            << setw(7) << setprecision(3) << DVOL[IP]
        << " UCW= "
            << setw(7) << setprecision(2) << multip->TMASSA[IP]
        << " U.C.Density = "
            << setw(7) << setprecision(3) << 1.66113*multip->TMASSA[IP]/VOL[IP]
        << " gr/cm^3" << endl
            << "                     _____________________________" << endl;
        //L10:;
    }
    // ****** QUANTITATIVE ANALYSIS ***************
    WTOTAL = 0.000000;
    //DWTOTAL = 0.000000;
    for (I = 1; I <= cntrls->NPHASE; ++I) WTOTAL = WTOTAL + W[I];
    for (I = 1; I <= cntrls->NPHASE; ++I)
    {
        //      !cp nov 10 96
        if (cntrls->NPHASE == 1)
        {
            XMASS[I] = 100.0 * W[I] / WTOTAL;
            DMASS[1]= 0.0;
        }
        else
        {
            XMASS[I] = 100.0 * W[I] / WTOTAL;
            DMASS[I] = 100.0 * DW[I];
        }
    }
    IINNMOL = 0;
    for (I = 1; I <= cntrls->NPHASE; ++I)
    {
        if (IINNMOL == 1) goto L2713;
        if (jnk->NMOL[I] == 0) IINNMOL=1;
L2713:;
    }
    if (IINNMOL == 1)
    {
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            // ** printing results
            file6 << "PHASE = "
                << setw(2) << I
                << " => %MASS = "
                << setw(6) << setprecision(2) << XMASS[I]
            << "(+/-)"
                << setw(6) << setprecision(2) << DMASS[I]
            << "  %MOLAR = NOT COMPUTED" << endl;
        }
    }
    else
    {
        // ****    CALCULATION OF MOLAR FRACTION  ****
        FT = 0.0000000;
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            FRP[I] = XMASS[I] * jnk->NMOL[I] / multip->TMASSA[I];
            FT = FT + FRP[I];
        }
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            FR[I] = 100.0 * FRP[I] / FT;
            DFR[I] = FR[I] * DMASS[I]/XMASS[I];
            // ** printing results
            file6 << "PHASE = "
                << setw(2) << I
                << " => %MASS = "
                << setw(6) << setprecision(2) << XMASS[I]
            << "(+/-)"
                << setw(6) << setprecision(2) << DMASS[I]
            << "  %MOLAR = "
                << setw(6) << setprecision(2) << FR[I]
            << "(+/-)"
                << setw(6) << setprecision(2) << DFR[I] << endl;
        }
    }
    if (cntrls->ISPHASE != 0)
    {
        file6 << "Considering Amorphous Content:" << endl;
        SFIS=multip->WTIS[cntrls->ISPHASE]/XMASS[cntrls->ISPHASE];
        if(SFIS > 1.0)
        {
            file6 << "PROBLEM:Amount of Internal Standard (Phase #"
                << setw(2) << cntrls->ISPHASE
                << ") is less than the specified "
                << setw(6) << setprecision(2) << multip->WTIS[cntrls->ISPHASE]
            << "%." << endl
                << "Amorphous content not computed. Check ISWT in line 11.2 for this phase" << endl;
            goto L2720;
        }
        for (I=1; I <= cntrls->NPHASE; ++I)
        {
            file6 << "PHASE = " << setw(2) << I
                << " => %MASS = " << setw(6) << setprecision(2) << XMASS[I]*SFIS << endl;
        }
        file6 << "AMORPHOUS  => %MASS = " << setw(6) << setprecision(2) << 100*(1.0-SFIS) << endl;
    }
L2720:
    file6 << endl;
    //L291:
    for (J=1; J <= 20; ++J)
    {
        allp->ILOC = allp->ILOC +1;
        KM=params->LGLB[J];
        if(KM != 0)goto L20;
        SY[J]=0.0;
        SZ[J]=0.0;
        allp->FINAL[allp->ILOC][1] = params->GLB[J];
        goto L19;
L20:
        SZ[J]=sqrt(abs(f1->RJAC[KM][KM]));
        SY[J]=f1->VX[KM]*params->AGLB[J]*params->RELAX[4];
        DUMMY[KM] = params->GLB[J];
        params->GLB[J]=params->GLB[J]+SY[J];
        DUMMY[KM+MSZ]  = SY[J];
        allp->FINAL[allp->ILOC][1] = params->GLB[J];
        allp->FINAL[allp->ILOC][2] = SZ[J];
L19:;
    }
    if (allp->ILOC  >  NFINAL) DBWSException("Parameter NFINAL in PARAM.INC file too small");
    file6 << "GLOBAL PARAMETERS" << endl
        << "ZEROPOINT (ZER)             :"
        << setw(8) << setprecision(4) << params->GLB[1]
    << setw(8) << setprecision(4) << SY[1]
    << setw(8) << setprecision(4) << SZ[1] << endl;
    file6 << "SAMPLE DISPLACEMENT (DISP)  :"
        << setw(8) << setprecision(4) << params->GLB[10]
    << setw(8) << setprecision(4) << SY[10]
    << setw(8) << setprecision(4) << SZ[10] << endl
        << "SAMPLE TRANSPARENCY (TRANSP):"
        << setw(8) << setprecision(4) << params->GLB[11]
    << setw(8) << setprecision(4) << SY[11]
    << setw(8) << setprecision(4) << SZ[11] << endl
        << "ROUGHNESS PARAMETERS        :"
        << "             P              :"
        << setw(8) << setprecision(4) << params->GLB[8]
    << setw(8) << setprecision(4) << SY[8]
    << setw(8) << setprecision(4) << SZ[8] << endl
        << "             Q              :"
        << setw(8) << setprecision(4) << params->GLB[9]
    << setw(8) << setprecision(4) << SY[9]
    << setw(8) << setprecision(4) << SZ[9] << endl
        << "             R              :"
        << setw(8) << setprecision(4) << params->GLB[12]
    << setw(8) << setprecision(4) << SY[12]
    << setw(8) << setprecision(4) << SZ[12] << endl
        << "             T              :"
        << setw(8) << setprecision(4) << params->GLB[13]
    << setw(8) << setprecision(4) << SY[13]
    << setw(8) << setprecision(4) << SZ[13] << endl;
    // !cp ap 20 97  !from It. codes
    file6 << "AMORPHOUS SCALE (SCAM):"
        << setw(11) << setprecision(4) << params->GLB[20]
    << setw(11) << setprecision(4) << SY[20]
    << setw(11) << setprecision(4) << SZ[20] << endl;
    // !cp ap 20 97   !from It. codes
    file6 << "MONOCROMATOR BANDPASS PARAMETERS (PMONI)" << endl
        << setw(8) << setprecision(4) << params->GLB[18]
    << setw(8) << setprecision(4) << SY[18]
    << setw(8) << setprecision(4) << SZ[18]
    << setw(8) << setprecision(4) << params->GLB[19]
    << setw(8) << setprecision(4) << SY[19]
    << setw(8) << setprecision(4) << SZ[19] << endl;
    if (jnk->NBCKGD != 0) goto L90;
    file6 << "BACKGROUND PARAMETERS" << endl //<< scientific
        << setw(12) << setprecision(6) << params->GLB[2]
    << setw(12) << setprecision(6) << SY[2]
    << setw(12) << setprecision(6) << SZ[2] << endl
        << setw(12) << setprecision(6) << params->GLB[3]
    << setw(12) << setprecision(6) << SY[3]
    << setw(12) << setprecision(6) << SZ[3] << endl
        << setw(12) << setprecision(6) << params->GLB[4]
    << setw(12) << setprecision(6) << SY[4]
    << setw(12) << setprecision(6) << SZ[4] << endl
        << setw(12) << setprecision(6) << params->GLB[5]
    << setw(12) << setprecision(6) << SY[5]
    << setw(12) << setprecision(6) << SZ[5] << endl
        << setw(12) << setprecision(6) << params->GLB[6]
    << setw(12) << setprecision(6) << SY[6]
    << setw(12) << setprecision(6) << SZ[6] << endl
        << setw(12) << setprecision(6) << params->GLB[7]
    << setw(12) << setprecision(6) << SY[7]
    << setw(12) << setprecision(6) << SZ[7] << endl;
L90:
    for (I=1; I <= cntrls->MAXS; ++I) file8o << DUMMY[I];
    for (I=MSZ+1; I <= MSZ+cntrls->MAXS; ++I) file8o << DUMMY[I];
    for (I=2*MSZ+1; I <= 2*MSZ+4; ++I) file8o << DUMMY[I];
    return;
    //L99990:
    //	DBWSException("ERROR IN WRITING TO UNIT 8 IN OUTPTR");
}

void DBWS::ITER(void)
{
    int I,J,K,L,IA,IX,IOF,IPM,LIM,IPREV,IDERIV,IORDLIM2;
    int KS[50+1];
    double X,X1,YY,SDK,DYC,OOM,STH,THX,CISK,FPOL,TOTAS,TOTCS,TOTDS;
    double CSK[99+1];
    double DISK[99+1];
    double DYCDD[99+1];
    double VLAST[MSZ+1];
    double DERISO[NATS+1];
    double ISODER[NATS+1];


    for (I=1; I <= MSZ; ++I) VLAST[I]=0.0;

    //     **** start cycles ****
    for (IX=1; IX <= cntrls->MCYCLE; ++ IX)
    {
        ASSIGN_();
        for (I=1; I <= MSZ; ++I)
        {
            f1->VX[I]=0.0;
            for (J=1; J <= MSZ; ++J) f1->RJAC[I][J]=0.0;
        }
        for (I=1; I <= NATS; ++I)
        {
            if (cntrls->IBGD == 1)
            {
                ISODER[I] = 1;
            }
            else
            {
                ISODER[I]=0;
            }
        }
        g3->NUM=0;
        IPREV=1;
        if(jnk->NBCKGD != 0)goto L30;
        g3->TH=g1->THMIN-g1->STEP;
        for (I=1; I <= datax->NPTS; ++I)
        {
            g3->TH=g3->TH+g1->STEP;
            THX=g3->TH/g1->BKPOS-1.0;
            datax->BK[I]=params->GLB[2];
            for (J=2; J <= 6; ++J) datax->BK[I]=datax->BK[I]+params->GLB[J+1] * pow(THX,J-1);
            if(cntrls->MCYCLE == 1 && cntrls->MAXS == 0 && cntrls->IPLPOL == 1) fondi->BKPOL[I]=datax->BK[I];
        }
L30:
        g3->TH=g1->THMIN-g1->STEP;

        //           ****** START GREAT LOOP ON NPTS STEP-POINTS ******
        for (IPM=1; IPM <= datax->NPTS; ++IPM)
        {
            if ( (IPM % (datax->NPTS/80+1)) == 0) cout << ".";
            g3->TH=g3->TH+g1->STEP;
            // !cp jun 97 test for bck option
            if (cntrls->IBGD == 1)
            {
                CSK[1]    = 1;
                DISK[1]   = 1;
                DYCDD[1]  = 1;
                TOTCS  = 1;
                goto L4334;
            }
            //-----COMPUTE FPOL = POLARIZATION FACTOR FOR THIS IPM POINT
            //     TH  = 2.0 * THETA
            //     STH = SIN(THETA)
            STH = sin(g3->TH * 0.008726646);
            FPOL = (1.0 + pow(1.0 - 2.0 * STH * STH,2) * g1->CTHM ) / 2.0;
            if(datax->KR[IPM] == IRS)
            {
                // ----IDERIV = 1 MEANS ONLY BACKGROUND (FOR THE STEP-POINTS THAT
                //                                       ARE IN EXCLUDED REGIONS)
                IDERIV = 1;
            }
            else
            {
                //-----IDERIV = 2 MEANS BACKGROUND + DERIVATIVES (FOR STEP-POINTS THAT ARE
                //                                                IN INCLUDED REGIONS)
                IDERIV = 2;
            }
            //-----if NECESSARY COMPUTE TOTCS :
            //     TOTCS = TOTAL COMPTON INTENSITY SCATTERED BY ALL CRYSTALLINE
            //             PHASES AT THE IPM-TH POINT OF THE X-RAY PATTERN.
            TOTCS = 0.0;
            if ( cntrls->FONDO == 1  ||  cntrls->FONDO == 2 )
            {
                for (K = 1; K <= cntrls->NPHASE; ++K)
                {
                    COMPTON(K,STH,&CISK);
                    CSK[K] = CISK * FPOL;
                    TOTCS = TOTCS + bkgscale->SCABKG[K] * CSK[K];
                }
                //-----COMPTON UPDATE
                if ( cntrls->MCYCLE == 1  &&  cntrls->MAXS == 0  &&  cntrls->IPLCOM == 1 )   fondi->BKCOM[IPM] = TOTCS;
                datax->BK[IPM] = datax->BK[IPM] + TOTCS;
            }
            //-----if NECESSARY CALL DIS = SUBROUTINE TO COMPUTE THERMAL AND LATTICE
            //                             DISORDER SCATTERING SDK  AND  DERIVATIVES
            //                             DYC IN THE K-TH PHASE
            //     TOTDS = TOTAL DISORDER SCATTERING
            TOTDS = 0.0;
            if (cntrls->FONDO == 1 || cntrls->FONDO == 2)
            {
                for (K = 1; K <= cntrls->NPHASE; ++K)
                {
                    IOF = 0;
                    if(K > 1)
                    {
                        for (I = 2; I <= K; ++I) IOF = IOF + jnk->NATOM[I-1];
                    }
                    for (I=1; I <= NATS; ++I) DERISO[I]=0;
                    DISORDER(K,STH,IDERIV,&SDK,&DYC,cntrls->FONDO,DERISO);
                    //int K, double STH, int IDERIV, double* SDK, double* DYC, int FONDO, double DERISO[])
                    DISK[K]  =   SDK * FPOL;
                    TOTDS    = TOTDS + bkgscale->SCABKG[K] * DISK[K];
                    //     UPDATING DERIVATE OF ISOTROPIC THERMAL FACTORS
                    if(cntrls->FONDO == 1)
                    {
                        for (I=1; I <= jnk->NATOM[K]; ++I) ISODER[IOF+I]= ISODER[IOF+I]+bkgscale->SCABKG[K]*FPOL*DERISO[IOF+I];
                    }
                    //     UPDATING DERIVATE OF OVERALL THERMAL FACTOR
                    if(cntrls->FONDO == 2) DYCDD[K]=DYC*bkgscale->SCABKG[K]*FPOL;
                }
                //-----DISORDER UPDATE
                if(cntrls->MCYCLE == 1  &&  cntrls->MAXS == 0  &&  cntrls->IPLDIS == 1 ) fondi->BKDIS[IPM] = TOTDS;
                datax->BK[IPM] = datax->BK[IPM] + TOTDS;
            }
            //------AMORPHOUS EVALUATIONS
            TOTAS =0.0;
            TOTAS = params->GLB[20] * datax->AMORPHOUS[IPM];
            //-----AMORPHOUS UPDATE
            if ( cntrls->MCYCLE == 1  &&  cntrls->MAXS == 0  &&  cntrls->IPLAM == 1 ) fondi->BKAM[IPM] = TOTAS;
            datax->BK[IPM] = datax->BK[IPM] + TOTAS;
L4334:
            if(datax->KR[IPM] == IRS )goto L9;
            g3->IORD1=datax->KR[IPM] % IRS;
            g3->IORD2=(datax->KR[IPM]/IRS) % IRS;
            if ( (g3->IORD2 == 0  ||  g3->IORD1 == 0)  &&  jnk->NBCKGD != 0 ) goto L9;
            //L15:
            g3->NUM=g3->NUM+1;
            IORDLIM2=g3->IORD2;
            if(IPREV > IORDLIM2) goto L40;
            for (J=IPREV; J <= IORDLIM2; ++J) CALCUL(J);
L40:
            IPREV=max(IPREV,IORDLIM2+1);
            //L108:
            SUMMAT(IPM,CSK,DISK,DYCDD,ISODER,TOTCS);
L9:;
        }
        cout << ":" << endl;
        if ( cntrls->JOBTYP > 2 ) return;
        for (I=1; I <= cntrls->MAXS; ++I)
        {
            for (J=I; J <= cntrls->MAXS; ++J) f1->RJAC[J][I] = f1->RJAC[I][J];
        }
        g3->COND = DPINV(f1->RJAC,f1->VX,&cntrls->MAXS);
        CHISQ();
        OOM=g2->S2 / static_cast<double>(g3->NUM-cntrls->MAXS);
        for (I=1; I <= cntrls->MAXS; ++I)
        {
            for (J=1; J <= cntrls->MAXS; ++J) f1->RJAC[I][J] = f1->RJAC[I][J]*OOM;
        }
        //              CODE TO ATTEMPT TO STABILIZE OSCILLATIONS
        for (I=1; I <= cntrls->MAXS; ++I)
        {
            if( sign(f1->VX[I]) == sign(VLAST[I])) goto L60;
            if(abs(f1->VX[I]) > 1.2*abs(VLAST[I])) goto L60;
            if(abs(f1->VX[I]) < 0.8*abs(VLAST[I])) goto L60;
            f1->VX[I]=f1->VX[I]/2.0;
L60:
            VLAST[I]=f1->VX[I];
        }
        OUTPTR(IX);
        for (I=1; I <= cntrls->MAXS; ++I)
        {
            X1=sqrt(abs(f1->RJAC[I][I]))*cntrls->EPS;
            if(abs(f1->VX[I]) > X1)goto L10;
        }
        if (cntrls->MAXS > 0)
        {
            file6 << endl
                << "          ***** EPSED OUT *****" << endl;
        }
        if (cntrls->MAXS > 0) cntrls->ICYRUN = IX;
        goto L20;
L10:;
    }
    //                 ***** END CYCLES *****
L20:
    //     CODE FOR PRINTING CORRELATION MATRIX MOVED FROM EXPUT (65-85)
    if(cntrls->MAT == 0 || cntrls->MAXS == 0) return;
    file6 << endl << endl << "CORRELATION MATRIX=" << endl;
    IA=1;
    LIM=19;
L38:
    LIM=min(cntrls->MAXS,LIM);
    for (I=IA; I <= LIM; ++I) file6 << setw(6) << I;
    file6 << endl;
    for (I=1; I <= cntrls->MAXS; ++I)
    {
        L=0;
        X=f1->RJAC[I][I];
        for (J=IA; J <= LIM; ++J)
        {
            L=L+1;
            YY=f1->RJAC[J][J]*X;
            KS[L]= static_cast<int>( 100.0*f1->RJAC[I][J]/(sqrt(abs(YY)) * sign(YY) )+0.5 );
        }
        file6 << setw(5) << I;
        for (J=1; J <= L; ++J) file6 << setw(6) << KS[J];
        file6 << endl;
    }
    if(LIM >= cntrls->MAXS) goto L37;
    IA=LIM+1;
    LIM=LIM+19;
    goto L38;
L37:;
}

void DBWS::CEL000(void)
{
    //
    //                             THIS SR IS USED TO FORCE A POINT XYZ TO
    //                           LIE IN THE UNIT CELL AT 000.
    //

    blnk1->Y[blnk1->MULTX-1][1] = fmod(blnk1->Y[blnk1->MULTX-1][1]+8.0,1.0);
    if ( blnk1->Y[blnk1->MULTX-1][1]-0.99999 < 0 ) goto L2; else goto L1;
L1:
    blnk1->Y[blnk1->MULTX-1][1] = 0.0;
L2:
    blnk1->Y[blnk1->MULTX-1][2] = fmod(blnk1->Y[blnk1->MULTX-1][2]+8.0,1.0);
    if ( blnk1->Y[blnk1->MULTX-1][2]-0.99999 < 0 ) goto L4; else goto L3;
L3:
    blnk1->Y[blnk1->MULTX-1][2] = 0.0;
L4:
    blnk1->Y[blnk1->MULTX-1][3] = fmod(blnk1->Y[blnk1->MULTX-1][3]+8.0,1.0);
    if ( blnk1->Y[blnk1->MULTX-1][3]-0.99999 < 0 ) goto L6; else goto L5;
L5:
    blnk1->Y[blnk1->MULTX-1][3] = 0.0;
L6:;
}

void DBWS::OPERTR(int* I, int* L2)
{
    //
    //                             THIS SR GENERATES THE SYMMETRY EQUIVALENT
    //                           POSITIONS. IT IS CALLED BY SYMOPR.
    //

    int I1,I2,I3,I4,I5,I6,I7,I8;

    I1 = (spgcom->NCONT[*I] % 4)+1;
    I2 = ((spgcom->NCONT[*I]/4) % 4)+1;
    I3 = ((spgcom->NCONT[*I]/16) % 4)+1;
    I4 = ((spgcom->NCONT[*I]/64) % 4)+1;
    I5 = ((spgcom->NCONT[*I]/256) % 2);
    I6 = ((spgcom->NCONT[*I]/512) % 4)+1;
    I7 = ((spgcom->NCONT[*I]/2048) % 4)+1;
    I8 = spgcom->NCONT[*I]/8192+1;
    switch (I8) {
    case 1:
        goto L100;
        break;
    case 2:
        goto L200;
        break;
    case 3:
        goto L300;
        break;
    }
    GOTOER();
L100:
    switch (I1) {
    case 1:
        goto L101;
        break;
    case 2:
        goto L102;
        break;
    case 3:
        goto L103;
        break;
    case 4:
        goto L104;
        break;
    }
    GOTOER();
L101:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[*L2][1];
    goto L105;
L102:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[*L2][2];
    goto L105;
L103:
    blnk1->Y[blnk1->MULTX][1] = -blnk1->Y[*L2][1];
    goto L105;
L104:
    blnk1->Y[blnk1->MULTX][1] = -blnk1->Y[*L2][2];
L105:
    switch (I2) {
    case 1:
        goto L110;
        break;
    case 2:
        goto L107;
        break;
    case 3:
        goto L108;
        break;
    case 4:
        goto L106;
        break;
    }
    GOTOER();
L106:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[blnk1->MULTX][1]+0.25;
L107:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[blnk1->MULTX][1]+0.25;
L108:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[blnk1->MULTX][1]+0.25;
L110:
    switch (I3) {
    case 1:
        goto L111;
        break;
    case 2:
        goto L112;
        break;
    case 3:
        goto L113;
        break;
    case 4:
        goto L114;
        break;
    }
    GOTOER();
L111:
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[*L2][2];
    goto L115;
L112:
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[*L2][1];
    goto L115;
L113:
    blnk1->Y[blnk1->MULTX][2] = -blnk1->Y[*L2][2];
    goto L115;
L114:
    blnk1->Y[blnk1->MULTX][2] = -blnk1->Y[*L2][1];
L115:
    switch (I4) {
    case 1:
        goto L120;
        break;
    case 2:
        goto L117;
        break;
    case 3:
        goto L118;
        break;
    case 4:
        goto L116;
        break;
    }
    GOTOER();
L116:
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[blnk1->MULTX][2]+0.25;
L117:
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[blnk1->MULTX][2]+0.25;
L118:
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[blnk1->MULTX][2]+0.25;
L120:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[*L2][3] * static_cast<double>(1-2*I5);
    switch (I6) {
    case 1:
        goto L125;
        break;
    case 2:
        goto L122;
        break;
    case 3:
        goto L123;
        break;
    case 4:
        goto L121;
        break;
    }
    GOTOER();
L121:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[blnk1->MULTX][3]+0.25;
L122:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[blnk1->MULTX][3]+0.25;
L123:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[blnk1->MULTX][3]+0.25;
L125:
    switch (I7) {
    case 1:
        goto L130;
        break;
    case 2:
        goto L127;
        break;
    case 3:
        goto L126;
        break;
    }
    GOTOER();
L126:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[blnk1->MULTX][3]+0.333333333;
L127:
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[blnk1->MULTX][3]+0.333333333;
L130:
    blnk1->MULTX = blnk1->MULTX+1;
    CEL000();
    return;
L200:
    blnk1->Y[blnk1->MULTX][1] = -blnk1->Y[*L2][2];
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[*L2][1]-blnk1->Y[*L2][2];
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[*L2][3]+0.333333333 * static_cast<double>(I2-2);
    blnk1->MULTX = blnk1->MULTX+1;
    CEL000();
    blnk1->Y[blnk1->MULTX][1] = -blnk1->Y[blnk1->MULTX-1][2];
    blnk1->Y[blnk1->MULTX][2] = -blnk1->Y[*L2][1];
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[*L2][3]-0.333333333 * static_cast<double>(I2-2);
    goto L130;
L300:
    I1 = *L2;
L301:
    blnk1->Y[blnk1->MULTX][1] = blnk1->Y[I1][3];
    blnk1->Y[blnk1->MULTX][2] = blnk1->Y[I1][1];
    blnk1->Y[blnk1->MULTX][3] = blnk1->Y[I1][2];
    if ( I1-*L2 <= 0 ) goto L302; else goto L130;
L302:
    I1 = blnk1->MULTX;
    blnk1->MULTX = blnk1->MULTX+1;
    CEL000();
    goto L301;
}

void DBWS::SYMOPR(void)
{
    //
    //                             THIS SR TAKES A POINT XYZ, DETERMINES THE
    //                           SITE MULTIPLICITY, THE SITE SYMMETRY, AND
    //                           THE SYMMETRY CONSTRAINTS ON THE POSITIONAL
    //                           AND THERMAL PARAMETERS.
    //

    int I,J,L1,L2,L3,L4,L5,L6,KX,KY,KZ,IOP,ISCK,ISRH;
    double X3;

    ISCK = 0;
L103:
    blnk1->MULTX = 2;
    ISRH = 0;
    blnk1->IXB = 0;
    blnk1->XLT = 1.0;
    J = 1;
    CEL000();
    L1=J;
    for (I=1; I <= 8; ++I)
    {
        IOP = 0;
        if ( spgcom->NCONT[I] == 0 ) goto L104;
        //L1:
        for (L2=J; L2 <= L1; ++L2)
        {
            L3=blnk1->MULTX-1;
            if ( spgcom->NCONT[I]-8192 == 0 ) goto L5; else goto L4;
L5:
            blnk1->Y[blnk1->MULTX][1]=blnk1->Y[L2][1]+1.0/3.0;
            blnk1->Y[blnk1->MULTX][2]=blnk1->Y[L2][2]+2.0/3.0;
            blnk1->Y[blnk1->MULTX][3]=blnk1->Y[L2][3]+2.0/3.0;
            blnk1->Y[blnk1->MULTX+1][1]=blnk1->Y[L2][1]+2.0/3.0;
            blnk1->Y[blnk1->MULTX+1][2]=blnk1->Y[L2][2]+1.0/3.0;
            blnk1->Y[blnk1->MULTX+1][3]=blnk1->Y[L2][3]+1.0/3.0;
            blnk1->MULTX=blnk1->MULTX+1;
            CEL000();
            blnk1->MULTX=blnk1->MULTX+1;
            CEL000();
            goto L6;
L4:
            OPERTR(&I,&L2);
L6:
            L5=blnk1->MULTX;
            L6=spgcom->NCONT[I];
            //L7:
            blnk1->MULTX = L3+1;
L8:
            L4 = 1;
            if ( abs(blnk1->Y[blnk1->MULTX][1]-blnk1->Y[L4][1])-0.0001 <= 0 ) goto L2; else goto L90;
L2:
            if ( abs(blnk1->Y[blnk1->MULTX][2]-blnk1->Y[L4][2])-0.0001 <= 0 ) goto L3; else goto L90;
L3:
            if ( abs(blnk1->Y[blnk1->MULTX][3]-blnk1->Y[L4][3])-0.0001 <= 0 ) goto L91; else goto L90;
L91:
            if ( blnk1->NCTR[blnk1->MULTX]-8192 < 0)
            {
                goto L92;
            }
            else if ( blnk1->NCTR[blnk1->MULTX]-8192 == 0)
            {
                goto L94;
            }
            else
            {
                goto L93;
            }
L92:
            KX = (blnk1->NCTR[blnk1->MULTX] % 4)+1;
            KY = ((blnk1->NCTR[blnk1->MULTX]/16) % 4)+1;
            KZ = (blnk1->NCTR[blnk1->MULTX]/256) % 2;
            if ( IOP == 0 ) blnk1->XLT=blnk1->XLT/2.0;
            IOP = 1;
            switch (KX) {
            case 1:
                goto L920;
                break;
            case 2:
                goto L930;
                break;

            case 3:
                goto L940;
                break;

            case 4:
                goto L950;
                break;
            }
            GOTOER();
L920:
            switch (KY) {
            case 1:
                goto L921;
                break;
            case 2:
                goto L1000;
                break;

            case 3:
                goto L922;
                break;

            case 4:
                goto L1000;
                break;
            }
            GOTOER();
L921:
            if ( KZ == 0 ) goto L1000;
            blnk1->IXB |= 2621450;
            goto L90;
L922:
            if ( KZ == 0 ) blnk1->IXB |= 2228292;
            if ( KZ == 1 ) blnk1->IXB |= 655432;
            goto L90;
L930:
            blnk1->IXB |= 32768;
            switch (KY) {
            case 1:
                goto L1000;
                break;
            case 2:
                goto L931;
                break;

            case 3:
                goto L1000;
                break;

            case 4:
                goto L932;
                break;
            }
            GOTOER();
L931:
            if ( KZ == 0 ) blnk1->IXB |= 4194464;
            if ( KZ == 1 ) blnk1->IXB |= 12583048;
            goto L90;
L932:
            blnk1->IXB |= 2753088;
            if ( KZ == 1 ) blnk1->IXB |= 8;
            goto L90;
L940:
            blnk1->IXB |= 512;
            switch (KY) {
            case 1:
                goto L941;
                break;
            case 2:
                goto L1000;
                break;

            case 3:
                goto L942;
                break;

            case 4:
                goto L1000;
                break;
            }
            GOTOER();
L941:
            blnk1->IXB |= 131072;
            if ( KZ == 1 ) goto L943;
            blnk1->IXB |= 525312;
            goto L90;
L942:
            blnk1->IXB |= 64;
            if ( KZ == 1 ) goto L944;
            blnk1->IXB |= 2623488;
            goto L90;
L943:
            blnk1->IXB |= 2097152;
L944:
            blnk1->IXB |= 8;
            goto L90;
L950:
            blnk1->IXB |= 32768;
            switch (KY) {
            case 1:
                goto L1000;
                break;
            case 2:
                goto L951;
                break;

            case 3:
                goto L1000;
                break;

            case 4:
                goto L952;
                break;
            }
            GOTOER();
L951:
            blnk1->IXB |= 2753088;
            if ( KZ == 1 ) blnk1->IXB |= 8;
            goto L90;
L952:
            if ( KZ == 0 ) blnk1->IXB |= 8388864;
            if ( KZ == 1 ) blnk1->IXB |= 4194568;
            goto L90;
L93:
            if ( (blnk1->NCTR[blnk1->MULTX] % 16384) == 0 ) goto L95;
            ISRH = 1;
            if ( (307 & (blnk1->NCTR[blnk1->MULTX]-spgcom->NCONT[I])) == 0 ) goto L92;
            goto L90;
L94:
            blnk1->IXB |= 2916928;
            blnk1->XLT = blnk1->XLT/3.0;
            goto L99;
L95:
            blnk1->IXB |= 17924240;
            blnk1->XLT = blnk1->XLT/3.0;
            goto L99;
L90:
            if ( L6-8192 < 0 ) goto L99; else goto L98;
L98:
            L6=1;
            ++blnk1->MULTX;
            goto L8;
L99:
            blnk1->MULTX=L5;
        }
        //L100:
        L1=blnk1->MULTX-1;
    }
L104:
    if ( ISRH == 0 ) goto L200;
    if ( ISCK > 1 ) goto L2000;
    ++ISCK;
    for (J=1; J <= I; ++J) if ( spgcom->NCONT[J] == 16384 ) goto L1011;
    for (L2 = 1; L2 <= L1; ++L2)
    {
        if ( abs(blnk1->Y[L2][1]-blnk1->Y[L2][2]) <= 0.0001 ) goto L102;
        if ( abs(blnk1->Y[L2][1]+blnk1->Y[L2][2]-1.0) <= 0.0002 ) goto L102;
    }
    ISCK = 2;
    file6 << "NO X,X,Z OR X,-X,Z SET WAS FOUND FOR"
        << setw(8) << setprecision(5) << blnk1->Y[1][1]
    << setw(8) << setprecision(5) << blnk1->Y[1][2]
    << setw(8) << setprecision(5) << blnk1->Y[1][3]
    << setw(8) << setprecision(5) << blnk1->XLT << endl;
    goto L200;
L1011:
    if ( abs(blnk1->Y[1][1]-blnk1->Y[1][2]) < 0.0001 ) goto L120;
    file6 << "THE ATOM AT "
        << setw(8) << setprecision(5) << blnk1->Y[1][1]
    << setw(8) << setprecision(5) << blnk1->Y[1][2]
    << setw(8) << setprecision(5) << blnk1->Y[1][3]
    << " WAS PUT AT"
        << setw(8) << setprecision(5) << blnk1->Y[1][2]
    << setw(8) << setprecision(5) << blnk1->Y[1][3]
    << setw(8) << setprecision(5) << blnk1->Y[1][1] << endl;
    X3 = blnk1->Y[1][1];
    blnk1->Y[1][1] = blnk1->Y[1][2];
    blnk1->Y[1][2] = blnk1->Y[1][3];
    blnk1->Y[1][3] = X3;
    goto L103;
L102:
    if ( L2 == 1 ) goto L120;
    file6 << "THE ATOM AT "
        << setw(8) << setprecision(5) << blnk1->Y[1][1]
    << setw(8) << setprecision(5) << blnk1->Y[1][2]
    << setw(8) << setprecision(5) << blnk1->Y[1][3]
    << " WAS PUT AT"
        << setw(8) << setprecision(5) << blnk1->Y[L2][1]
    << setw(8) << setprecision(5) << blnk1->Y[L2][2]
    << setw(8) << setprecision(5) << blnk1->Y[L2][3] << endl;
    blnk1->Y[1][1] = blnk1->Y[L2][1];
    blnk1->Y[1][2] = blnk1->Y[L2][2];
    blnk1->Y[1][3] = blnk1->Y[L2][3];
    goto L103;
L1000:
    file6 << "THE SYMMETRY OPERATOR " << blnk1->NCTR[blnk1->MULTX] << " IS WRONG" << endl;
    DBWSException("7701");
L2000:
    file6 << "THERE ARE UNRESOLVABLE PROBLEMS WITH THE POSITION SET"
        << setw(8) << setprecision(5) << blnk1->Y[1][1]
    << setw(8) << setprecision(5) << blnk1->Y[1][2]
    << setw(8) << setprecision(5) << blnk1->Y[1][3] << endl
        << "ATTEMPTS TO DEFINE THE TRUE SITE SYMMETRY WERE ABANDONED." << endl;
L120:
    if ( abs(blnk1->Y[1][1]-blnk1->Y[1][2])+abs(blnk1->Y[1][1]-blnk1->Y[1][3]) <= 0.0002 ) goto L200;
    for (L2=2; L2 <= L1; ++L2) if ( abs(blnk1->Y[L2][1]-blnk1->Y[L2][2])+abs(blnk1->Y[L2][1]-blnk1->Y[L2][3]) <= 0.0002 ) goto L102;
    //L130:
L200:
    blnk1->IXB = max( (blnk1->IXB & 18874367), (blnk1->IXB & 16777215));
    if (  (blnk1->IXB & 384) == 384 ) blnk1->IXB |= 512;
}

void DBWS::RTMT(int* IPRT, int* IPHASE)
{
    //    THE MATRICES ARE IN THE FIRST THREE ROWS OF VCTR AND THE VECTORS
    //    ARE IN THE FOURTH ROW OF VCTR.
    //    THE MATRICES ARE PACKED INTO IVEC.

    int I,K,IX,IY,IZ;
    double VCTR[3+1][4+1];


    blnk1->Y[1][1] = 2.0/108.0;
    blnk1->Y[1][2] = 3.0/108.0;
    blnk1->Y[1][3] = 4.0/108.0;
    SYMOPR();
    spgcom->MULTP = blnk1->MULTX-1;
    multip->MLTPHASE=spgcom->MULTP;
    for (I=1; I <= spgcom->MULTP; ++I)
    {
        rtmtx->IVEC[*IPHASE][I] = 0;
        for (K=1; K <= 3; ++K)
        {
            IX = static_cast<int>(blnk1->Y[I][K]*108.0+0.1);
            VCTR[K][4] = static_cast<double>((IX+4)/9) /12.0;
            VCTR[K][4] = fmod(VCTR[K][4],1.0);
            IY = ((IX+4) % 9)-4;
            IZ = abs(IY);
            switch (IZ) {
            case 1:
                goto L220;
                break;
            case 2:
                goto L230;
                break;
            case 3:
                goto L240;
                break;
            case 4:
                goto L250;
                break;
            }
            GOTOER();
L220:
            VCTR[K][1] = -1.0 * sign(IY);
            VCTR[K][2] =  1.0 * sign(IY);
            VCTR[K][3] =  0.0;
            goto L300;
L230:
            VCTR[K][1] = 1.0 * sign(IY);
            VCTR[K][2] = 0.0;
            VCTR[K][3] = 0.0;
            goto L300;
L240:
            VCTR[K][1] = 0.0;
            VCTR[K][2] = 1.0 * sign(IY);
            VCTR[K][3] = 0.00;
            goto L300;
L250:
            VCTR[K][1] = 0.0;
            VCTR[K][2] = 0.0;
            VCTR[K][3] = 1.0 * sign(IY);
L300:
            rtmtx->IVEC[*IPHASE][I] = rtmtx->IVEC[*IPHASE][I]+(
            9*(static_cast<int>(VCTR[K][1]+1.5))+
            3*(static_cast<int>(VCTR[K][2]+1.5))+
            static_cast<int>(VCTR[K][3]+1.5))*32768 * static_cast<int>(pow(32,3-K))+
            (static_cast<int>(VCTR[K][4]*12.0+.5)+16) * static_cast<int>(pow(32,3-K));
        }

        if ( *IPRT <= 0 ) goto L310;
        if(simoper->ISIMOP == 1)goto L310;
        if ( (I-1 % 15) == 0) file6 << "The operations of the space group are" << endl;
        file6
            << " ("	<< setw(3) << setprecision(0) << VCTR[1][1]	<< setw(3) << setprecision(0) << VCTR[1][2]	<< setw(3) << setprecision(0) << VCTR[1][3]	<< " )   ( X )   ("	<< setw(6) << setprecision(3) << VCTR[1][4] << " )   ( X2 )" << endl
            << " ("	<< setw(3) << setprecision(0) << VCTR[2][1]	<< setw(3) << setprecision(0) << VCTR[2][2]	<< setw(3) << setprecision(0) << VCTR[2][3]	<< " ) * ( Y ) + (" << setw(6) << setprecision(3) << VCTR[2][4] << " ) = ( Y2 )" << endl
            << " ("	<< setw(3) << setprecision(0) << VCTR[3][1]	<< setw(3) << setprecision(0) << VCTR[3][2]	<< setw(3) << setprecision(0) << VCTR[3][3]	<< " )   ( Z )   (" << setw(6) << setprecision(3) << VCTR[3][4] << " )   ( Z2 )"
            << "          VEC(" << setw(3) << I << ") =" << setw(15) << rtmtx->IVEC[*IPHASE][I] << endl << endl;
L310:;
    }
    rtmtx->MLTPHS[*IPHASE]=spgcom->MULTP;
    rtmtx->ICNTPHS[*IPHASE]=1;
    if ( spgcom->NC == 0 ) rtmtx->ICNTPHS[*IPHASE]=2;
}

void DBWS::OP1(int* IPHASE)
{
    //
    //                            THIS SR GENERATES THE OPERATORS FOR
    //                           SMTRY2, DETERMINES THE POLARITY OF THE SPACE
    //                           GROUP, NOTES THE PRESENCE OF A CENTER(1BAR),
    //                           AND GENERATES A SET OF PSEUDO-OPERATORS FOR
    //                           USE IN SYMOPR TO DEFINE SITE SYMMETRY.

    int I,J,K,L,N2;

    K = 2;
    spgcom->NPOL = 7;
    for (I=1; I <= 7; ++I) hklctl->NC1[1][I][*IPHASE] = 0;
    blnk1->NCTR[1] = 0;
    for (I=1; I <= 8; ++I)
    {
        if ( spgcom->NCONT[I] <= 0 ) goto L101;
        if ( spgcom->NCONT[I] < 8192 ) goto L80;
        if ( spgcom->NCONT[I] <= 8192 ) goto L60;		// TODO: Possivel erro aqui estava 08192
        if ( spgcom->NCONT[I] == 16384 ) goto L70;
        spgcom->NPOL = 4;
        hklctl->NC1[I][1][*IPHASE] = 2;
L50:
        for (J=2; J <= 7; ++J) hklctl->NC1[I][J][*IPHASE] = 0;
        hklctl->NC1[I][6][*IPHASE] = (spgcom->NCONT[I]/4) % 32;
        goto L90;
L60:
        hklctl->NC1[I][1][*IPHASE] = 3;
        spgcom->NPOL = 1;
        goto L50;
L70:
        hklctl->NC1[I][1][*IPHASE] = 1;
        goto L50;
L80:
        hklctl->NC1[I][1][*IPHASE] = 0;
        hklctl->NC1[I][2][*IPHASE] = spgcom->NCONT[I] % 4;
        hklctl->NC1[I][3][*IPHASE] = (spgcom->NCONT[I]/4) % 4;
        hklctl->NC1[I][4][*IPHASE] = (spgcom->NCONT[I]/16) % 4;
        hklctl->NC1[I][5][*IPHASE] = (spgcom->NCONT[I]/64) % 4;
        hklctl->NC1[I][6][*IPHASE] = (spgcom->NCONT[I]/256) % 2;
        hklctl->NC1[I][7][*IPHASE] = (spgcom->NCONT[I]/512) % 16;
L90:
        L = K-1;
        for (J=1; J <= L; ++J)
        {
            if ( spgcom->NCONT[I] == 8192 ) goto L95;
            blnk1->NCTR[K] = (spgcom->NCONT[I] ^ blnk1->NCTR[J]) & 57651;
            if( (spgcom->NCONT[I] & 17) == 0) goto L92;
            if( (blnk1->NCTR[J] & 34) == 0) goto L92;
            if( (blnk1->NCTR[J] & 34) == 34) goto L92;
            blnk1->NCTR[K]= blnk1->NCTR[K] ^ 34;
L92:
            ++K;
            if ( spgcom->NCONT[I] < 8192 ) goto L100;
            blnk1->NCTR[K] = blnk1->NCTR[K-1]+32768;
            K = K+1;
            goto L100;
L95:
            blnk1->NCTR[K] = 0;
            blnk1->NCTR[K+1] = 0;
            K = K+2;
L100:;
        }
    }
L101:
    spgcom->MULTP = K-1;
    hklctl->N1HKL[*IPHASE] = I-1;
    if ( hklctl->N1HKL[*IPHASE] < 2 ) goto L105;
    for (N2=2; N2 <= hklctl->N1HKL[*IPHASE]; ++N2) if ( hklctl->NC1[N2-1][1][*IPHASE] == 1 ) goto L103;
    goto L105;
L103:
    for (I=N2; I <= hklctl->N1HKL[*IPHASE]; ++I)
    {
        for (J=1; J <= 7; ++J) hklctl->NC1[I-1][J][*IPHASE] = hklctl->NC1[I][J][*IPHASE];
    }
    hklctl->NC1[hklctl->N1HKL[*IPHASE]][1][*IPHASE] = 1;
L105:
    spgcom->NC = 1;
    for (I=1; I <= spgcom->MULTP; ++I)
    {
        if ( (blnk1->NCTR[I] ^ 290) == 0 ) spgcom->NC = 0;
        if ( (blnk1->NCTR[I] & 3) > 0 ) spgcom->NPOL = (spgcom->NPOL & 6);
        if ( (blnk1->NCTR[I] & 48) > 0 ) spgcom->NPOL = (spgcom->NPOL & 5);
        if ( (blnk1->NCTR[I] & 256) > 0 ) spgcom->NPOL = (spgcom->NPOL & 3);
    }
}

void DBWS::LOOKUP(int K, int N, int NSCAT, int IXRAY, int JOB)
{
    int I,J,L,NS,IOF,NSL,IIPHAS,TBXPTR;

    IOF=0;
    NS=0;
    if (K > 1)
    {
        for (IIPHAS=2; IIPHAS <= K; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
    }
    if (K > 1) NS=dc->NSAVE;
    if (JOB == 2 || JOB == 4) goto L70;
    for (I=1; I <= N; ++I)
    {
        NS=max(NSCAT,NS);
        for (J=1; J <= NS; ++J) if (parac->NTYP[I+IOF] == coefc->NAM[J]) goto L30;
        for (J=1; J <= 212; ++J) if (parac->NTYP[I+IOF] == TBXC[J]) goto L50;
        file6 << " SCATTERING COEFFICIENTS NOT FOUND FOR " << parac->NTYP[I+IOF] << endl;
        //41      FORMAT(' SCATTERING COEFFICIENTS NOT FOUND FOR ',A4)
        DBWSException("SCATTERING DATA MISSING");
L30:
        params->PTR[I+IOF]=J;
        goto L10;
L50:
        NS=NS+1;
        params->PTR[I+IOF]=NS;
        for (L=1; L <= 9; ++ L) coeff->AC[L][NS]=TBX[J][L];
        coefc->NAM[NS]=TBXC[J];
        TBXPTR=static_cast<int>(TBX[J][10]);			//TBXPTR=static_cast<int>(TBX[J][10]+0.5);
        coeff->DFP[NS]=TBD[TBXPTR][IXRAY];
        coeff->DFPP[NS]=TBD[TBXPTR][IXRAY+10];
        coeff->XMAS[NS]=TBM[TBXPTR];
L10:;
    }
    dc->NSAVE=NS;
    return;
L70:
    for (I=1; I <= N; ++I)
    {
        NS=max(NSCAT,NS);
        for (J=1; J <= NS; ++J) if(parac->NTYP[I+IOF] == coefc->NAM[J])goto L100;
        for (J=1; J <= 85; ++J) if(parac->NTYP[I+IOF] == TABNC[J])goto L120;
        file6 << " SCATTERING LENGTHS NOT FOUND FOR " << parac->NTYP[I+IOF] << endl;
        //111     FORMAT(34H SCATTERING LENGTHS NOT FOUND FOR ,A4)
        DBWSException("SCATTERING DATA MISSING");
L100:
        params->PTR[I+IOF]=J;
        goto L80;
L120:
        NS=NS+1;
        params->PTR[I+IOF]=NS;
        coeff->DFP[NS]=TABN[J];
        coefc->NAM[NS]=TABNC[J];
        if (J  >=  61  &&  J  <=  81) goto L81;
        if (J  ==  82) goto L82;
        if (J  >=  83  &&  J  <=  85) goto L83;
        goto L84;
L81:
        NSL = J+2;
        goto L180;
L82:
        NSL = 90;
        goto L180;
L83:
        NSL = J+9;
        goto L180;
L84:
        NSL = J;
L180:
        coeff->XMAS[NS]=TBM[NSL];
L80:;
    }
    dc->NSAVE=NS;
}

void DBWS::CELL2(int NPHASE, double LAMDAM)
{
    const string LAU[14+1] = {"",  "1BAR","2/M","MMM","4/M","4/MMM","3BAR   R","3BAR M R","3BAR","3BAR M 1","3BAR 1 M","6/M","6/MMM","M3","M3M"};

    double COSA,COSB,COSC,SINA,SINB,SINC,SINASR,SINBSR,COSASR,COSBSR,COSCSR;


    file6 << "THE LAUE SYMMETRY IS " << LAU[spgcom->NSPGRP] << endl;
    if ( spgcom->NAXIS > 3 ) DBWSException("5001");
    switch (spgcom->NSPGRP) {
    case 1:
        goto L1002;
        break;
    case 2:
        goto L1071;
        break;
    case 3:
        goto L1072;
        break;
    case 4:
        goto L1073;
        break;
    case 5:
        goto L1073;
        break;
    case 6:
        goto L1074;
        break;
    case 7:
        goto L1074;
        break;
    case 8:
        goto L1075;
        break;
    case 9:
        goto L1075;
        break;
    case 10:
        goto L1075;
        break;
    case 11:
        goto L1075;
        break;
    case 12:
        goto L1075;
        break;
    case 13:
        goto L1076;
        break;
    case 14:
        goto L1076;
        break;
    }
    GOTOER();
L1071:
    if ( spgcom->NAXIS != 1 ) cellx->ALPHA=90.0;
    if ( spgcom->NAXIS != 2 ) cellx->BETA=90.0;
    if ( spgcom->NAXIS != 3) cellx->GAMMA=90.0;
    goto L1002;
L1073:
    cellx->B = cellx->A;
L1072:
    cellx->ALPHA = 90.0;
    cellx->BETA = 90.0;
    cellx->GAMMA = 90.0;
    goto L1002;
L1074:
    cellx->B = cellx->A;
    cellx->C = cellx->A;
    cellx->BETA = cellx->ALPHA;
    cellx->GAMMA = cellx->ALPHA;
    goto L1002;
L1075:
    cellx->B = cellx->A;
    cellx->ALPHA = 90.;
    cellx->BETA = 90.0;
    cellx->GAMMA = 120.0;
    goto L1002;
L1076:
    cellx->B = cellx->A;
    cellx->C = cellx->A;
    goto L1072;
L1002:
    COSA = cos(6.28318531*cellx->ALPHA/360.0);
    //L105:
    COSB = cos(6.28318531*cellx->BETA/360.0);
    //L1052:
    COSC = cos(6.28318531*cellx->GAMMA/360.0);
    //L1053:
    SINA = sin(6.28318531*cellx->ALPHA/360.0);		// SINA=sqrt(1.0-pow(COSA,2));
    SINB = sin(6.28318531*cellx->BETA/360.0);		// SINB=sqrt(1.0-pow(COSB,2));
    SINC = sin(6.28318531*cellx->GAMMA/360.0);		// SINC=sqrt(1.0-pow(COSC,2));




    //
    //-----THE VOLUME OF THE CELL IN THE DIRECT SPACE OF THE REAL CELL OF
    //-----THE K-TH PHASE=NPHASE IS CALCULATED
    //-----AND THE RAY GMAX OF THE SPHERE WITH THE VOLUME EQUIVALENT TO THE
    //-----BRILLOUIN CELL 4/3*PI*GMAX**3=1/VOL

    // Volume of cell in direct space
    volume->VOLI[NPHASE] = cellx->A * cellx->B * cellx->C * sqrt(1.0 - pow(COSA,2) - pow(COSB,2) - pow(COSC,2) + 2.0 * COSA * COSB * COSC );

    volume->GCOM[NPHASE] = 0.877298169 * volume->VOLI[NPHASE] / pow(LAMDAM,3);

    // cos alpha in space reciprocal. Giacovazzo, p67 table 2.1
    COSASR=(COSB*COSC-COSA)/(SINB*SINC);

    // cos beta in space reciprocal. Giacovazzo, p67 table 2.1
    COSBSR=(COSA*COSC-COSB)/(SINA*SINC);

    // cos gamma in space reciprocal. Giacovazzo, p67 table 2.1
    COSCSR=(COSA*COSB-COSC)/(SINA*SINB);

    // TODO: Verifica se isso esta funcionando em double
    SINASR=sqrt(1.0-pow(COSASR,2));
    SINBSR=sqrt(1.0-pow(COSBSR,2));

    // a*, a in space reciprocal. Giacovazzo, p67 table 2.1
    //
    // sin(betha*) = V/(a*b*c*sin(alpha)*sin(gamma))
    //
    // a* = b*c*sin(alpha)/V
    //
    // a* = 1/(sin(beta*)*sin(gamma)*a)
    //
    cellx->AL[1][1]=1.0/(cellx->A * SINC * SINBSR);

    // b*, a in space reciprocal. Giacovazzo, p67 table 2.1
    cellx->AL[2][2]=1.0/(cellx->B * SINC * SINASR);

    // c*, a in space reciprocal. Giacovazzo, p67 table 2.1
    cellx->AL[3][3]=1.0/(cellx->C * SINA * SINBSR);

    cellx->AL[2][3]=cellx->AL[2][2]*cellx->AL[3][3]*COSASR*2.0;
    cellx->AL[1][3]=cellx->AL[3][3]*cellx->AL[1][1]*COSBSR*2.0;
    cellx->AL[1][2]=cellx->AL[2][2]*cellx->AL[1][1]*COSCSR*2.0;

    cellx->AL[1][1]=cellx->AL[1][1]*cellx->AL[1][1];
    cellx->AL[2][2]=cellx->AL[2][2]*cellx->AL[2][2];
    cellx->AL[3][3]=cellx->AL[3][3]*cellx->AL[3][3];
}

double DBWS::MULT(int IH, int IK, int IL, int KXIS)
{
    double P;

    P = 2.0;
    switch (spgcom->NSPGRP)
    {
    case 1:
        goto L220;
        break;
    case 2:
        goto L100;
        break;
    case 3:
        goto L110;
        break;
    case 4:
        goto L120;
        break;
    case 5:
        goto L130;
        break;
    case 6:
        goto L150;
        break;
    case 7:
        goto L180;
        break;
    case 8:
        goto L140;
        break;
    case 9:
        goto L170;
        break;
    case 10:
        goto L160;
        break;
    case 11:
        goto L170;
        break;
    case 12:
        goto L190;
        break;
    case 13:
        goto L200;
        break;
    case 14:
        goto L210;
        break;
    }
    GOTOER();
L100:
    if ( IH <= 0 ) goto L220; else goto L101;
L101:
    if ( KXIS == 3 )
    {
        if ( IL > 0  &&  IH+abs(IK) > 0 ) P=2.0*P;
    }
    else
    {
        if ( IK > 0  &&  IH+abs(IL) > 0 ) P=2.0*P;
    }
    goto L220;
L110:
    P = 1.0;
    if ( IH <= 0 ) goto L112; else goto L111;
L111:
    P = 2.0*P;
L112:
    if ( IK <= 0) goto L114; else goto L113;
L113:
    P = 2.0*P;
L114:
    if ( IL <= 0 ) goto L220; else goto L115;
L115:
    P = 2.0*P;
    goto L220;
L120:
    if ( IK <= 0 ) goto L220; else goto L121;
L121:
    P = 2.0*P;
    if ( IL <= 0 ) goto L220; else goto L122;
L122:
    P = 2.0*P;
    goto L220;
L130:
    if ( IK <= 0) goto L220; else goto L131;
L131:
    P = 2.0*P;
    if ( IH <= 0 ) goto L134; else goto L132;
L132:
    if ( IH-IK < 0 ) goto L133; else goto L134;
L133:
    P = 2.0*P;
L134:
    if ( IL <= 0 ) goto L220; else goto L135;
L135:
    P = 2.0*P;
    goto L220;
L140:
    if ( IH+abs(IK) <= 0 ) goto L142; else goto L141;
L141:
    P = 3.0*P;
L142:
    if ( IL <= 0 ) goto L220; else goto L143;
L143:
    P = 2.0*P;
    goto L220;
L150:
    if ( IH+IK+IL == 0 ) goto L152; else goto L151;
L151:
    P = 2.0*P;
L152:
    if ( IH-IK == 0 ) goto L154; else goto L153;
L153:
    P = 3.0*P;
    goto L220;
L154:
    if ( IH-IL == 0) goto L220; else goto L153;
L160:
    if ( IK <= 0 ) goto L220; else goto L161;
L161:
    P = 3.0*P;
    if ( IK-IH <= 0 ) goto L220; else goto L162;
L162:
    if ( IH <= 0 ) goto L220; else goto L135;
L170:
    if ( IL <= 0 ) goto L172; else goto L171;
L171:
    P = 2.0*P;
L172:
    if ( IH <= 0 ) goto L174; else goto L173;
L173:
    P = 3.0*P;
    if ( IK <= 0 ) goto L220; else goto L135;
L174:
    if ( IK <= 0 ) goto L220; else goto L153;
L180:
    if ( IH-IK == 0 ) goto L181; else goto L182;
L181:
    if ( IH-IL == 0 ) goto L220; else goto L183;
L182:
    if ( IK-IL == 0 ) goto L183; else goto L184;
L183:
    P = P/2.0;
L184:
    P = 3.0*P;
    if ( IH+IK+IL == 0 ) goto L220; else goto L135;
L190:
    if ( IK <= 0 ) goto L194; else goto L191;
L191:
    P = 6.0*P;
    if ( IH == 0 ) goto L194; else goto L192;
L192:
    if ( IK-IH == 0 ) goto L194; else goto L193;
L193:
    P = 2.0*P;
L194:
    if ( IL == 0 ) goto L220; else goto L135;
L200:
    if ( IH-IK == 0 ) goto L201; else goto L203;
L201:
    if ( IK-IL == 0 ) goto L202; else goto L203;
L202:
    P = 4.0*P;
    goto L220;
L203:
    P = 3.0*P;
    if ( IK <= 0 ) goto L205; else goto L204;
L204:
    P = 2.0*P;
L205:
    if ( IH <= 0 ) goto L220; else goto L135;
L210:
    if ( IH == 0 ) goto L214; else goto L211;
L211:
    P = 4.0*P;
    if ( IH-IL == 0 ) goto L220; else goto L212;
L212:
    P = 3.0*P;
    if ( IH-IK == 0 ) goto L220; else goto L213;
L213:
    if ( IK-IL == 0 ) goto L220; else goto L135;
L214:
    P = 3.0*P;
    if ( IK == 0 ) goto L220; else goto L215;
L215:
    P = 2.0*P;
    if ( IK-IL == 0 ) goto L220; else goto L135;
    //-----THIS LINE ADDED TO CORRECT THE PROBLEMS WITH THE -3, -3M1, 6/M,
    //-----AND 6/MMM SPACE GROUPS THAT WERE OFF BY A FACTOR OF TWO
L220:
    if (spgcom->NSPGRP == 8  ||  spgcom->NSPGRP == 9  ||  spgcom->NSPGRP == 11  ||  spgcom->NSPGRP == 12) P=0.5*P;
    return P;
}

void DBWS::REWRIT(int ISCALE, int IDIF)
{
    int I, J, K, N, IOF, ISOF, NPLOF, IIPHAS;

    file5.close();
    file5b.open(file5name.data(),ios::trunc);			//	REWIND 5;
    file5b << fixed;


    // line 1
    file5b << char_->TITLE << "          OpenDBWS" <<endl;
    cntrls->JOBTYP=cntrls->JOBTYP-1;
    //cntrls->NPROF=cntrls->NPROF-1;
    NPLOF = cntrls->NPROF;
    if (sizestrain->NSIZESTRAIN == 9) NPLOF = 9;
    cntrls->INSTRM = cntrls->INSTRM-1;

    // line 2.1
    // line 2.1 changed due to size-strain calculation (NsizeStrain)
    file5b << setw(4) << cntrls->JOBTYP
        << setw(4) << NPLOF
        << setw(4) << cntrls->NPHASE
        << setw(4) << codebck->IBCKCODE
        << setw(4) << jnk->NEXCRG
        << setw(4) << jnk->NSCAT
        << setw(4) << cntrls->INSTRM
        << setw(4) << cntrls->IPREF
        << setw(4) << cntrls->IASYM
        << setw(4) << cntrls->IABSR
        << setw(4) << cntrls->IDATA
        << setw(4) << cntrls->ISPHASE
        << setw(4) << cntrls->I2D94
        << "     LINE 2.1" << endl;

    // line 2.2
    if(codebck->IBCKCODE == -1)
    {
        file5b << setw(4) << cntrls->IAS
            << setw(4) << cntrls->FONDO
            << "                                                 LINE 2.2" << endl;
    }

    // line 3
    file5b
        << setw(1) << cntrls->IOT
        << setw(1) << cntrls->IPL
        << setw(1) << cntrls->IPC
        << setw(1) << cntrls->MAT
        << setw(1) << cntrls->NXT
        << " "
        << setw(1) << cntrls->LST1
        << setw(1) << cntrls->LST2
        << setw(1) << cntrls->LST3
        << setw(1) << cntrls->IPL1
        << setw(1) << cntrls->IPL2
        << " "
        << setw(1) << cntrls->IPLST
        << setw(1) << cntrls->IPLOSS
        << setw(1) << cntrls->IPLCAL
        << setw(1) << cntrls->IPLPOL
        << setw(1) << cntrls->IPLCOM
        << " "
        << setw(1) << cntrls->IPLDIS
        << setw(1) << cntrls->IPLAM
        << setw(1) << cntrls->IPBIG
        << "                                    LINE 3" << endl;

    // line 4
    file5b
        << setw(8) << setprecision(5) << g1->LAMDA[1]
        << setw(8) << setprecision(5) << g1->LAMDA[2]
        << setw(8) << setprecision(5) << params->RATIO[2]
        << setw(8) << setprecision(4) << g1->BKPOS
        << setw(8) << setprecision(4) << g1->WDT
        << setw(8) << setprecision(4) << g1->CTHM
        << setw(8) << setprecision(4) << g1->TMV
        << setw(8) << setprecision(4) << g1->RLIM
        << setw(8) << setprecision(4) << cntrls->SW
        << endl;

    // line 5
    file5b
        << setw(4) << cntrls->MCYCLE
        << setw(4) << setprecision(2) << cntrls->EPS
        << setw(4) << setprecision(2) << params->RELAX[1]
        << setw(4) << setprecision(2) << params->RELAX[2]
        << setw(4) << setprecision(2) << params->RELAX[3]
        << setw(4) << setprecision(2) << params->RELAX[4]
        << "                                 CYCLS EPS RELAX P_CALC" << endl;
    if(jnk->NBCKGD < 2)goto L120;

    // line 6(*)
    for (I=1; I <= jnk->NBCKGD; ++I) file5b << setw(8) << setprecision(2) << jnk->POS[I] << jnk->BCK[I] << endl;
L120:
    if(jnk->NEXCRG <= 0)goto L122;

    // line 7(*)
    for (I=1; I <= jnk->NEXCRG; ++I)
        file5b
            << setw(8) << setprecision(2) << jnk->ALOW[I]
            << setw(8) << setprecision(2) << jnk->AHIGH[I]
            << "                                         EXCLUDED REGION" << endl;
L122:
    if (jnk->NSCAT <= 0) goto L124;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        if (cntrls->JOBTYP  ==  1 || cntrls->JOBTYP == 3) goto L1228;
        //C line 8.1 XRD (*)
        file5b
            << setw(4) << coefc->NAM[I]
            << setw(8) << setprecision(4) << coeff->DFP[I]
            << setw(8) << setprecision(4) << coeff->DFPP[I]
            << setw(8) << setprecision(4) << coeff->XMAS[I]
            << "                             SCATTERING SET" << setw(2) << jnk->NSCAT << endl;
        goto L126;
        // line 8.1 ND(*)
L1228:
        file5b
            << setw(4) << coefc->NAM[I]
            << setw(8) << setprecision(4) << coeff->DFP[I]
            << setw(8) << setprecision(4) << coeff->XMAS[I]
            << "                                     SCATTERING SET " << setw(2) << jnk->NSCAT << endl;
        // line 8.2 XRD(*)
L126:
        if (cntrls->JOBTYP == 0 || cntrls->JOBTYP == 2)
        {
            for (J=1; J <= 9; ++J) file5b << setw(8) << setprecision(5) << coeff->AC[J][I];
            file5b << endl;
        }
    }
L124:;

    // line 9
    file5b << setw(8) << cntrls->MAXS << "                                                 PARAMS REFINED" << endl;
    N=0;
    for (IIPHAS=1; IIPHAS <= cntrls->NPHASE; ++IIPHAS) N=N+jnk->NATOM[IIPHAS];
    for (I=1; I <= N; ++I)
    {
        for (J=1; J <= 11; ++J) params->A[I][J]=sign(params->A[I][J])*(static_cast<double>(10*params->LP[I][J])+abs(params->A[I][J]));
    }
    for (I=1; I <= cntrls->NPHASE; ++I)
    {
        for (J=1; J <= 6; ++J) params->PAR[I][J+5]=dc->SAVE[I][J];
        for (J=1; J <= 27; ++J) params->APAR[I][J]=sign(params->APAR[I][J])*(static_cast<double>(10*params->LPAR[I][J])+abs(params->APAR[I][J]));
    }
    for (J=1; J <= 20; ++J) params->AGLB[J]=sign(params->AGLB[J])*(static_cast<double>(10*params->LGLB[J])+abs(params->AGLB[J]));

    // line 10.1
    file5b
        << setw(8) << setprecision(4) << params->GLB[1]
        << setw(8) << setprecision(4) << params->GLB[10]
        << setw(8) << setprecision(4) << params->GLB[11]
        << setw(8) << setprecision(4) << params->GLB[8]
        << setw(8) << setprecision(4) << params->GLB[9]
        << setw(8) << setprecision(4) << params->GLB[12]
        << setw(8) << setprecision(4) << params->GLB[13] << " ZER DISP TRANS p q r t" << endl;

    // line 10.11
    file5b
        << setw(8) << setprecision(4) << params->GLB[1]
        << setw(8) << setprecision(4) << params->AGLB[10]
        << setw(8) << setprecision(4) << params->AGLB[11]
        << setw(8) << setprecision(4) << params->AGLB[8]
        << setw(8) << setprecision(4) << params->AGLB[9]
        << setw(8) << setprecision(4) << params->AGLB[12]
        << setw(8) << setprecision(4) << params->AGLB[13] << " CODEWORDS" << endl;
    if (cntrls->IBGD == 1) goto L4600;
    // line 10.2
    file5b
        << setw(8) << setprecision(4) << params->GLB[20]
        << setw(8) << setprecision(4) << params->GLB[18]
        << setw(8) << setprecision(4) << params->GLB[19]
        << "                                 AM MON1 MON2" << endl;

    // line 10.21
    file5b
        << setw(8) << setprecision(4) << params->AGLB[20]
        << setw(8) << setprecision(4) << params->AGLB[18]
        << setw(8) << setprecision(4) << params->AGLB[19]
        << "                                 CODEWORS" << endl;
L4600:
    if(jnk->NBCKGD == 0)
    {
        file5b
            << setw(9) << setprecision(2) << params->GLB[2]
            << setw(9) << setprecision(2) << params->GLB[3]
            << setw(9) << setprecision(2) << params->GLB[4]
            << setw(9) << setprecision(2) << params->GLB[5]
            << setw(9) << setprecision(2) << params->GLB[6]
            << setw(9) << setprecision(2) << params->GLB[7]
            << "   BACKGROUND" << endl;
        file5b
            << setw(9) << setprecision(2) << params->AGLB[2]
            << setw(9) << setprecision(2) << params->AGLB[3]
            << setw(9) << setprecision(2) << params->AGLB[4]
            << setw(9) << setprecision(2) << params->AGLB[5]
            << setw(9) << setprecision(2) << params->AGLB[6]
            << setw(9) << setprecision(2) << params->AGLB[7]
            << "   CODEWORDS" << endl;
    }
    //L477:
    for (K=1; K <= cntrls->NPHASE; ++K)
    {
        IOF=0;
        if (K > 1)
        {
            for (IIPHAS=2; IIPHAS <= K; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }
        // line 11.1
        file5b << setw(50) << char_->PHSNM[K] << "       PHASE NUMBER " << setw(2) << K << endl;

        // line 11.2
        file5b
            << setw(4) << jnk->NATOM[K]
            << setw(4) << jnk->NMOL[K]
            << setw(7) << setprecision(4) << multip->SAQF[K]
            << " "
            << setw(4) << setprecision(1) << jnk->PREF[K][1]
            << setw(4) << setprecision(1) << jnk->PREF[K][2]
            << setw(4) << setprecision(1) << jnk->PREF[K][3]
            << setw(7) << setprecision(2) << multip->WTIS[K]
            << "                      #ATMS #FU AFQPA PREFDIR ISWT" << endl;
        N=jnk->NATOM[K];

        // line 11.3
        file5b << setw(20) << char_->SYMB[K] << "                                     SPACE GROUP" << endl;
        // Changing N to 'so' !cp oct 96
        for (ISOF=1; ISOF <= N; ++ISOF) params->XL[ISOF+IOF][5]=params->XL[ISOF+IOF][5]*multip->XMLTP[K]/multip->MURT[ISOF+IOF];
        // !cp oct 96 #6 murt parametrs included below. FORMAT modified...
        for (I=1; I <= N; ++I)
        {
            file5b
                << setw(4) << parac->ATEXT[I+IOF] << " "
                << setw(4) << multip->MURT[I+IOF] << " "
                << setw(4) << parac->NTYP[I+IOF] << "  "
                << setw(8) << setprecision(5) << params->XL[I+IOF][1]
                << setw(8) << setprecision(5) << params->XL[I+IOF][2]
                << setw(8) << setprecision(5) << params->XL[I+IOF][3]
                << setw(8) << setprecision(5) << params->XL[I+IOF][4]
                << setw(8) << setprecision(5) << params->XL[I+IOF][5]
                << "  LBL M NTYP x y z B So" << endl
                << "                "
                << setw(8) << setprecision(5) << params->A[I+IOF][1]
                << setw(8) << setprecision(5) << params->A[I+IOF][2]
                << setw(8) << setprecision(5) << params->A[I+IOF][3]
                << setw(8) << setprecision(5) << params->A[I+IOF][4]
                << setw(8) << setprecision(5) << params->A[I+IOF][5]
                << "  CODEWORDS" << endl
                << setw(8) << setprecision(5) << params->XL[I+IOF][6]
                << setw(8) << setprecision(5) << params->XL[I+IOF][7]
                << setw(8) << setprecision(5) << params->XL[I+IOF][8]
                << setw(8) << setprecision(5) << params->XL[I+IOF][9]
                << setw(8) << setprecision(5) << params->XL[I+IOF][10]
                << setw(8) << setprecision(5) << params->XL[I+IOF][11]
                << "          BETAS" << endl
                << setw(8) << setprecision(2) << params->A[I+IOF][6]
                << setw(8) << setprecision(2) << params->A[I+IOF][7]
                << setw(8) << setprecision(2) << params->A[I+IOF][8]
                << setw(8) << setprecision(2) << params->A[I+IOF][9]
                << setw(8) << setprecision(2) << params->A[I+IOF][10]
                << setw(8) << setprecision(2) << params->A[I+IOF][11]
            << "          CODEWORDS" << endl;
        }
        file5b
            << scientific << setw(8) << setprecision(2) << params->PAR[K][1]
            << fixed
            << setw(8) << setprecision(4) << params->PAR[K][2]
            << "                                         SCALE Bo(OVERALL)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][1]
            << setw(8) << setprecision(2) << params->APAR[K][2] << endl
            << setw(8) << setprecision(5) << params->PAR[K][3]
            << setw(8) << setprecision(5) << params->PAR[K][4]
            << setw(8) << setprecision(5) << params->PAR[K][5]
            << setw(8) << setprecision(5) << params->PAR[K][21]
            << setw(8) << setprecision(5) << params->PAR[K][20]
            << setw(8) << setprecision(5) << params->PAR[K][15]
            << setw(8) << setprecision(5) << params->PAR[K][16]
            << " U V W CT Z X Y" << endl

            << setw(8) << setprecision(2) << params->APAR[K][3]
            << setw(8) << setprecision(2) << params->APAR[K][4]
            << setw(8) << setprecision(2) << params->APAR[K][5]
            << setw(8) << setprecision(2) << params->APAR[K][21]
            << setw(8) << setprecision(2) << params->APAR[K][20]
            << setw(8) << setprecision(2) << params->APAR[K][15]
            << setw(8) << setprecision(2) << params->APAR[K][16] << endl

            << setw(8) << setprecision(4) << params->PAR[K][6]
            << setw(8) << setprecision(4) << params->PAR[K][7]
            << setw(8) << setprecision(4) << params->PAR[K][8]
            << setw(8) << setprecision(4) << params->PAR[K][9]
            << setw(8) << setprecision(4) << params->PAR[K][10]
            << setw(8) << setprecision(4) << params->PAR[K][11]
            << "         CELL PARAMETERS" << endl

            << setw(8) << setprecision(2) << params->APAR[K][6]
            << setw(8) << setprecision(2) << params->APAR[K][7]
            << setw(8) << setprecision(2) << params->APAR[K][8]
            << setw(8) << setprecision(2) << params->APAR[K][9]
            << setw(8) << setprecision(2) << params->APAR[K][10]
            << setw(8) << setprecision(2) << params->APAR[K][11] << endl

            << setw(8) << setprecision(5) << params->PAR[K][12]
            << setw(8) << setprecision(5) << params->PAR[K][13]
            << setw(8) << setprecision(5) << params->PAR[K][14]
            << "                                 PREF1 PREF2 R/RCF_ASYM" << endl

            << setw(8) << setprecision(2) << params->APAR[K][12]
            << setw(8) << setprecision(2) << params->APAR[K][13]
            << setw(8) << setprecision(2) << params->APAR[K][14] << endl

            << setw(8) << setprecision(4) << params->PAR[K][17]
            << setw(8) << setprecision(4) << params->PAR[K][18]
            << setw(8) << setprecision(4) << params->PAR[K][19]
            << "                                 NA NB NC (MIX_PARAMS)" << endl

            << setw(8) << setprecision(2) << params->APAR[K][17]
            << setw(8) << setprecision(2) << params->APAR[K][18]
            << setw(8) << setprecision(2) << params->APAR[K][19] << endl

            << setw(8) << setprecision(4) << params->PAR[K][24]
            << setw(8) << setprecision(4) << params->PAR[K][25]
            << setw(8) << setprecision(4) << params->PAR[K][26]
            << "                                 NA NB NC (HIGH SIDE)" << endl

            << setw(8) << setprecision(2) << params->APAR[K][24]
            << setw(8) << setprecision(2) << params->APAR[K][25]
            << setw(8) << setprecision(2) << params->APAR[K][26] << endl

            << setw(8) << setprecision(4) << params->PAR[K][27] << "" << endl

            << setw(8) << setprecision(2) << params->APAR[K][27] << endl;
    }
    if (cntrls->IPL != 0) file5b << setw(8) << ISCALE << IDIF << "                                         LINE PRINTER INFO" << endl;
    if (file5b.is_open()) file5b.close();
    //L151:
    cntrls->JOBTYP=cntrls->JOBTYP+1;
}

// subroutine to compute size&strain (NsizeStrain)
void DBWS::size(int K)
{
    double A7, B7, C7, D7,HD, UA, XA, YA, ZA, HS, UI, XI, YI, ZI, HDG, HDL, HSG, HSL,RADO;
    int ISIZETEST,ISTRAINTEST;
    ifstream file17;
    string s;

    RADO=3.1415927/180.0;
    A7 = 2.69269;
    B7 = 2.42843;
    C7 = 4.47163;
    D7 = 0.07842;
    file17.open("instr.dat");
    getline(file17,s);				// na primeira linha de padrao.dat sera apenas para comentario
    // a segunda linha ira ler U,Z,X,Y da FWHM do padrao
    file17 >> UI >> ZI >> XI >> YI;
    file17.close();
    UA = params->PAR[K][3];
    ZA = params->PAR[K][20];
    XA = params->PAR[K][15];
    YA = params->PAR[K][16];
    if (ZI  !=  0.0)
    {
        file6 << "SIZE NOT COMPUTED * bad standard? - check Z for standard." << endl;
        cout << "SIZE NOT COMPUTED * bad standard? - check Z for standard." << endl;
        goto L20;
    }
    if(ZA  <  0.0  ||  YA  <  YI)
    {
        file6 << "Phase " << setw(2) << K << ": Size-strain not Computed * check Z and',' Y for sample and standard" << endl;
        cout << "Phase " << setw(2) << K << ": Size-strain not Computed * check Z and',' Y for sample and standard" << endl;
        goto L20;
    }
    if(YA  ==  YI  &&  ZA  == 0.0)
    {
        file6 << "Phase " << setw(2) << K << ": Infinite Size * check Z and Y values',' for sample and standard" << endl;
        cout << "Phase " << setw(2) << K << ": Infinite Size * check Z and Y values',' for sample and standard" << endl;
        ISIZETEST = 1;
    }
    else
    {
        ISIZETEST = 0;
    }
    //
    // COMPUTE SIZE
    //
    //        COMPUTE SIZE BASED ON Gauss-COS Z parameter [sizeG(k)]
    //
    if(ZA > 0.0)
    {
        HSG = sqrt(ZA)*RADO;
        sizestrain->SIZEG[K] =  g1->LAMDA[1] / HSG;
    }
    else
    {
        HSG = 0.0;
        sizestrain->SIZEG[K] = 99999.0;
    }

    //
    //        COMPUTE SIZE BASED ON Loren-COS Y parameter [sizeL(k)]
    //  params->PAR[K][16] = Y (Lorentz)
    //
    if(YA  >  YI)
    {
        HSL = (YA - YI) * RADO;
        sizestrain->SIZEL[K] =  g1->LAMDA[1] / HSL;
    }
    else
    {
        HSL = 0.0;
        sizestrain->SIZEL[K] = 99999.0;
    }
    //
    //        COMPUTE WEIGHTED SIZE BASED on Gauss & Lorentz [siz(k)]
    //
    if(ISIZETEST == 0)
    {
        HS  = pow((pow(HSG,5) + A7 * pow(HSG,4) * HSL + B7 * pow(HSG,3) * pow(HSL,2) + C7 * pow(HSG,2) * pow(HSL,3) + D7 * HSG * pow(HSL,4) + pow(HSL,5)),0.2);
        sizestrain->SIZ[K] = g1->LAMDA[1] / HS;
    }
    else
    {
        sizestrain->SIZ[K] = 0.0;
    }
    //
    //       STRAIN COMPUTATIONS
    //L10:
    if(UA  <  UI  ||  XA  <  XI)
    {
        file6 << "Phase " << setw(2) << K << ": Size-Strain not computed * check U and X for sample and standard" << endl;
        cout << "Phase " << setw(2) << K << ": Size-Strain not computed * check U and X for sample and standard" << endl;
        goto L20;
    }
    if(XA  ==  XI  &&  UA  ==  UI)
    {
        file6 << "Phase " << setw(2) << K << ": Strain not Present * check U and X values for sample and standard" << endl;
        cout << "Phase " << setw(2) << K << ": Strain not Present * check U and X values for sample and standard" << endl;
        ISTRAINTEST = 1;
    }
    else
    {
        ISTRAINTEST = 0;
    }

    //
    // COMPUTE STRAIN
    //
    // compute strain based on Gauss-U * [strainG(k)]
    //
    if(UA  >  UI)
    {
        HDG = sqrt( (UA - UI) )*RADO;
        sizestrain->STRAING[K] =  HDG / 2;
    }
    else
    {
        HDG = 0.0;
        sizestrain->STRAING[K] = 0.0;
    }
    //
    // compute strain based on Lorentz-X * [strainL(k)]
    //
    if(XA  >  XI)
    {
        HDL = (XA - XI) * RADO;
        sizestrain->STRAINL[K] =  HDL / 2;
    }
    else
    {
        HDL = 0.0;
        sizestrain->STRAINL[K] = 0.0;
    }
    // compute weighted strain: based on Gauss-U and Lorentz-X * [strain(k)]
    //
    if (ISTRAINTEST == 0)
    {
        HD  = pow(( pow(HDG,5) + A7 * pow(HDG,4) * HDL + B7 * pow(HDG,3) * pow(HDL,2) + C7 * pow(HDG,2) * pow(HDL,3) + D7 * HDG * pow(HDL,4) + pow(HDL,5)),0.2);
        sizestrain->STRAIN[K] = HD / 2;
    }
    else
    {
        sizestrain->STRAIN[K] = 0.0;
    }
    file6 << "Gauss(phase " << setw(2) << K << "): Size =" //<< scientific
        << setw(9) << setprecision(4) << sizestrain->SIZEG[K]
    << " Strain ="
        << setw(9) << setprecision(3) << sizestrain->STRAING[K] << endl
        << "Lorentz(phase " << setw(2) << K << "): Size ="
        << setw(9) << setprecision(4) << sizestrain->SIZEL[K]
    << " Strain ="
        << setw(9) << setprecision(3) << sizestrain->STRAINL[K] << endl
        << "Weighted(phase " << setw(K) << "): Size ="
        << setw(9) << setprecision(4) << sizestrain->SIZ[K]
    << " Strain ="
        << setw(9) << setprecision(3) << sizestrain->STRAIN[K] << endl;
L20:
    return;
}

//      SUBROUTINE WRIT94 Gera uma saida para o 9411, pra poder Usar no ATOMS
void DBWS::WRITE94(int ISCALE, int IDIF)
{
    int I, J, K, N, IOF, IIPHAS;

    //REWIND 53;

    // line 1
    file53 << setw(70) << char_->TITLE << endl;
    cntrls->JOBTYP=cntrls->JOBTYP-1;
    cntrls->INSTRM = cntrls->INSTRM-1;

    // line 2.1
    file53 << setw(9) << setprecision(4) << cntrls->JOBTYP
        << setw(9) << setprecision(4) << cntrls->NPROF
        << setw(9) << setprecision(4) << cntrls->NPHASE
        << setw(9) << setprecision(4) << codebck->IBCKCODE
        << setw(9) << setprecision(4) << jnk->NEXCRG
        << setw(9) << setprecision(4) << jnk->NSCAT
        << setw(9) << setprecision(4) << cntrls->INSTRM
        << setw(9) << setprecision(4) << cntrls->IPREF
        << setw(9) << setprecision(4) << cntrls->IABSR
        << "                     LINE 2.1" << endl;

    // line 3
    file53 << setw(1) << cntrls->IOT
        << setw(1) << cntrls->IPL
        << setw(1) << cntrls->IPC
        << setw(1) << cntrls->MAT
        << setw(1) << cntrls->NXT
        << setw(1) << cntrls->LST1
        << setw(1) << cntrls->LST2
        << setw(1) << cntrls->LST3
        << setw(1) << cntrls->IPL1
        << setw(1) << cntrls->IPL2
        << setw(1) << cntrls->IPLST
        << "                                              LINE 3" << endl;

    // line 4
    file53 << setw(8) << setprecision(5) << g1->LAMDA[1]
    << setw(8) << setprecision(5) << g1->LAMDA[2]
    << setw(8) << setprecision(5) << params->RATIO[2]
    << setw(8) << setprecision(4) << g1->BKPOS
        << setw(8) << setprecision(4) << g1->WDT
        << setw(8) << setprecision(4) << g1->CTHM
        << setw(8) << setprecision(4) << g1->TMV
        << setw(8) << setprecision(4) << g1->RLIM << endl;

    // line 5
    file53 << setw(4) << cntrls->MCYCLE
        << setw(4) << setprecision(2) << cntrls->EPS
        << setw(4) << setprecision(2) << params->RELAX[1]
    << setw(4) << setprecision(2) << params->RELAX[2]
    << setw(4) << setprecision(2) << params->RELAX[3]
    << setw(4) << setprecision(2) << params->RELAX[4]
    << "                                 CYCLS EPS RELAX P_CALC" << endl;
    if (jnk->NBCKGD < 2) goto L120;

    // line 6(*)
    for (I=1; I <= jnk->NBCKGD; ++I)
    {
        file53 << setw(8) << setprecision(2) << jnk->POS[I]
        << setw(8) << setprecision(2) << jnk->BCK[I] << endl;
    }
L120:
    if(jnk->NEXCRG <= 0)goto L122;
    // line 7(*)
    for (I=1; I <= jnk->NEXCRG; ++I)
    {
        file53 << setw(8) << setprecision(2) << jnk->ALOW[I]
        << setw(8) << setprecision(2) << jnk->AHIGH[I] << endl;
    }
L122:
    if (jnk->NSCAT <= 0) goto L124;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        if (cntrls->JOBTYP  ==  1 || cntrls->JOBTYP == 3) goto L1228;
        // line 8.1 XRD (*)
        file53 << setw(4) << coefc->NAM[I]
        << setw(8) << setprecision(4) << coeff->DFP[I]
        << setw(8) << setprecision(4) << coeff->DFPP[I]
        << setw(8) << setprecision(4) << coeff->XMAS[I]
        << "                             SCATTERING SET " << setw(2) << jnk->NSCAT << endl;
        goto L126;
L1228:
        // line 8.1 ND(*)
        file53 << setw(4) << coefc->NAM[I]
        << setw(8) << setprecision(4) << coeff->DFP[I]
        << setw(8) << setprecision(4) << coeff->XMAS[I]
        << "                                     SCATTERING SET " << setw(2) << jnk->NSCAT << endl;
L126:
        // line 8.2 XRD(*)
        if(cntrls->JOBTYP == 0 || cntrls->JOBTYP == 2)
        {
            for (J=1; J <= 9; ++J) file53 << setw(8) << setprecision(5) << coeff->AC[J][I];
            file53 << endl;
        }
    }
L124:

    // line 9
    file53 << setw(8) << cntrls->MAXS << "                                                 PARAMS REFINED" << endl;
    N=0;
    for (IIPHAS=1; IIPHAS <= cntrls->NPHASE; ++IIPHAS) N=N+jnk->NATOM[IIPHAS];
    for (I=1; I <= N; ++I)
    {
        for (J=1; J <= 11; ++J) params->A[I][J]=sign(params->A[I][J])*(static_cast<double>(10*params->LP[I][J])+abs(params->A[I][J]));
    }
    for (I=1; I <= cntrls->NPHASE; ++I)
    {
        for (J=1; J <= 6; ++J) params->PAR[I][J+5]=dc->SAVE[I][J];
        for (J=1; J <= 27; ++J) params->APAR[I][J]=sign(params->APAR[I][J])*(static_cast<double>(10*params->LPAR[I][J])+abs(params->APAR[I][J]));
    }
    for (J=1; J <= 20; ++J) params->AGLB[J]=sign(params->AGLB[J])*(static_cast<double>(10*params->LGLB[J])+abs(params->AGLB[J]));

    // line 10.1
    file53 << setw(8) << setprecision(4) << params->GLB[1]
    << setw(8) << setprecision(4) << params->AGLB[1]
    << setw(8) << setprecision(4) << params->GLB[10]
    << setw(8) << setprecision(4) << params->AGLB[10]
    << setw(8) << setprecision(4) << params->GLB[11]
    << setw(8) << setprecision(4) << params->AGLB[11]
    << "         ZER DISP TRANS + CODEWORS" << endl
        << setw(9) << setprecision(4) << 0.0
        << setw(9) << setprecision(4) << 0.0
        << setw(9) << setprecision(4) << 0.0
        << setw(9) << setprecision(4) << 0.0
        << setw(9) << setprecision(4) << 0.0
        << setw(9) << setprecision(4) << 0.0
        << "   CODEWORDS" << endl;
    //L477:
    for (K=1; K <= cntrls->NPHASE; ++K)
    {
        IOF=0;
        if(K > 1)
        {
            for (IIPHAS=2; IIPHAS <= K; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }
        // line 11.1
        file53 << setw(50) << char_->PHSNM[K] << "       PHASE NUMBER " << setw(2) << K << endl;

        // line 11.2
        file53 << setw(4) << jnk->NATOM[K]
        << setw(4) << jnk->NMOL[K]
        << "        "
            << setw(4) << setprecision(1) << jnk->PREF[K][1]
        << setw(4) << setprecision(1) << jnk->PREF[K][2]
        << setw(4) << setprecision(1) << jnk->PREF[K][3]
        << "                             #ATMS #FU PREFDIR" << endl;
        N=jnk->NATOM[K];

        // line 11.3
        file53 << setw(20) << char_->SYMB[K]
        << "                                     SPACE GROUP" << endl;
        for (I=1; I <= N; ++I)
        {
            file53 << setw(4) << parac->ATEXT[I+IOF]
            << setw(4) << parac->NTYP[I+IOF]
            << "        "
                << setw(8) << setprecision(5) << params->XL[I+IOF][1]
            << setw(8) << setprecision(5) << params->XL[I+IOF][2]
            << setw(8) << setprecision(5) << params->XL[I+IOF][3]
            << setw(8) << setprecision(5) << params->XL[I+IOF][4]
            << setw(8) << setprecision(5) << params->XL[I+IOF][5]
            << "  LBL NTYP x y z B So" << endl
                << "                "
                << setw(8) << setprecision(2) << params->A[I+IOF][1]
            << setw(8) << setprecision(2) << params->A[I+IOF][2]
            << setw(8) << setprecision(2) << params->A[I+IOF][3]
            << setw(8) << setprecision(2) << params->A[I+IOF][4]
            << setw(8) << setprecision(2) << params->A[I+IOF][5]
            << "  CODEWORDS" << endl
                << setw(8) << setprecision(5) << params->XL[I+IOF][6]
            << setw(8) << setprecision(5) << params->XL[I+IOF][7]
            << setw(8) << setprecision(5) << params->XL[I+IOF][8]
            << setw(8) << setprecision(5) << params->XL[I+IOF][9]
            << setw(8) << setprecision(5) << params->XL[I+IOF][10]
            << setw(8) << setprecision(5) << params->XL[I+IOF][11]
            << "          BETAS" << endl
                << setw(8) << setprecision(2) << params->A[I+IOF][6]
            << setw(8) << setprecision(2) << params->A[I+IOF][7]
            << setw(8) << setprecision(2) << params->A[I+IOF][8]
            << setw(8) << setprecision(2) << params->A[I+IOF][9]
            << setw(8) << setprecision(2) << params->A[I+IOF][10]
            << setw(8) << setprecision(2) << params->A[I+IOF][11]
            << "          CODEWORDS" << endl;
        }
        file53 << scientific
            << setw(8) << setprecision(2) << params->PAR[K][1]
        << fixed
            << setw(8) << setprecision(4) << params->PAR[K][2]
        << "                                    SCALE Bo(OVERALL)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][1]
        << setw(8) << setprecision(2) << params->APAR[K][2] << endl
            << setw(8) << setprecision(5) << params->PAR[K][3]
        << setw(8) << setprecision(5) << params->PAR[K][4]
        << setw(8) << setprecision(5) << params->PAR[K][5]
        << setw(8) << setprecision(5) << params->PAR[K][21]
        << setw(8) << setprecision(5) << params->PAR[K][20]
        << setw(8) << setprecision(5) << params->PAR[K][15]
        << setw(8) << setprecision(5) << params->PAR[K][16]
        << " U V W CT Z X Y" << endl
            << setw(8) << setprecision(2) << params->APAR[K][3]
        << setw(8) << setprecision(2) << params->APAR[K][4]
        << setw(8) << setprecision(2) << params->APAR[K][5]
        << setw(8) << setprecision(2) << params->APAR[K][21]
        << setw(8) << setprecision(2) << params->APAR[K][20]
        << setw(8) << setprecision(2) << params->APAR[K][15]
        << setw(8) << setprecision(2) << params->APAR[K][16] << endl
            << setw(8) << setprecision(4) << params->PAR[K][6]
        << setw(8) << setprecision(4) << params->PAR[K][7]
        << setw(8) << setprecision(4) << params->PAR[K][8]
        << setw(8) << setprecision(4) << params->PAR[K][9]
        << setw(8) << setprecision(4) << params->PAR[K][10]
        << setw(8) << setprecision(4) << params->PAR[K][11]
        << "         CELL PARAMETERS" << endl
            << setw(8) << setprecision(2) << params->APAR[K][6]
        << setw(8) << setprecision(2) << params->APAR[K][7]
        << setw(8) << setprecision(2) << params->APAR[K][8]
        << setw(8) << setprecision(2) << params->APAR[K][9]
        << setw(8) << setprecision(2) << params->APAR[K][10]
        << setw(8) << setprecision(2) << params->APAR[K][11] << endl
            << setw(8) << setprecision(5) << params->PAR[K][12]
        << setw(8) << setprecision(5) << params->PAR[K][13]
        << setw(8) << setprecision(5) << params->PAR[K][14]
        << "                                 PREF1 PREF2 R/RCF_ASYM" << endl
            << setw(8) << setprecision(2) << params->APAR[K][12]
        << setw(8) << setprecision(2) << params->APAR[K][13]
        << setw(8) << setprecision(2) << params->APAR[K][14] << endl
            << setw(8) << setprecision(4) << params->PAR[K][17]
        << setw(8) << setprecision(4) << params->PAR[K][18]
        << setw(8) << setprecision(4) << params->PAR[K][19]
        << "                                 NA NB NC (MIX_PARAMS)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][17]
        << setw(8) << setprecision(2) << params->APAR[K][18]
        << setw(8) << setprecision(2) << params->APAR[K][19] << endl
            << setw(8) << setprecision(4) << params->PAR[K][24]
        << setw(8) << setprecision(4) << params->PAR[K][25]
        << setw(8) << setprecision(4) << params->PAR[K][26]
        << "                                 NA NB NC (HIGH SIDE)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][24]
        << setw(8) << setprecision(2) << params->APAR[K][25]
        << setw(8) << setprecision(2) << params->APAR[K][26] << endl
            << setw(8) << setprecision(2) << params->PAR[K][27]
        << "                                                 PEARSON ASYM.FACTOR" << endl
            << setw(8) << setprecision(2) << params->APAR[K][27] << endl;
    }

    if (file5.is_open()) file5.close();
    file5b.open(file5name.data());
    if (cntrls->IPL != 0) file5b << setw(8) << ISCALE << setw(8) << IDIF << "                                         LINE PRINTER INFO" << endl;
    file5.close();

    //L151:
    //                       JOBTYP  must be reproduced !!!
    cntrls->JOBTYP=cntrls->JOBTYP+1;
}

void DBWS::EXPUT(void)
{
    const char ISPACE		= ' ';
    const char IDOT			= '.';
    const char ISTAR		= '*';
    const char IPLUS		= '+';
    const char IMINUS		= '0';
    const char IBAR			= '-';

    int I,J,K,L,N,J20,JJ,IX,IS,NP,IY,IRH,IRK,IRL,ICZ,IRC,MIN,MAX,IXX,
        IDIF,J20IK,IEXC,NCOL,IXXX,NCOL1,NPTS2,ISCALE,
        IIPHAS,NPAGES,IFINIS,NLINES,ISTART,INUMB;
    double Z,BB,TH,TH2,HW,YX,RAD,TLG,TLL,TIC,RMIN,RMAX,XXXX,
        T2OBS,AFCAL,BFCAL,OMEGA,TFCAL,AFOBS,BFOBS,TDOBS,RFDNR,SHIFT,TIOBS,
        TFOBS,TLABEL,RFNMR;
    string IOUT,TITLE1,TITLE2,TITLE3,TITLE4,TITLE6,TITLE7,TITLE8,OUTFILE;
    bool VERT;
    int KI[10+1];
    double DEL[10+1];
    int LABEL[12+1];
    double DUMMY[2*MSZ+1 +1];
    string s;
    ofstream file69,file690,file9,file10,file31,file32,file33,file34,file37,file36,file38;



    ISCALE = 0;
    IDIF = 0;
    TITLE1 = "OSS.COR\"";
    TITLE2 = "TOTALE \"";
    TITLE3 = "COM.TOT\"";
    TITLE4 = "DIS.TOT\"";
    TITLE6 = "AM.TOT \"";
    TITLE7 = "POL.TOT\"";
    TITLE8 = "ALL.TOT\"";
    //if (cntrls->NXT != 0 || cntrls->JOBTYP >= 3) cntrls->NPROF=cntrls->NPROF-1;
    RAD = 45.0/atan(1.0);
    //	ZER = params->GLB[1];
    if (cntrls->IPL != 0)
    {
        getline(file5,s);
        stringstream(s.substr(0,8)) >> ISCALE;
        stringstream(s.substr(8,8)) >> IDIF;
    }
    //L10000:
    if (cntrls->NXT != 0 && cntrls->JOBTYP < 3) REWRIT(ISCALE,IDIF);
    if (cntrls->IPL2 != 0)
    {
        file69.open("bragg.dat");
        file690.open("xy-int.dat");
        file690 << "  2theta_i      y_o       y_c        yo-yc  w(yo-yc)^2" << endl;
    }
    if (cntrls->JOBTYP > 2) goto L37;
    file6 << endl
        << "AVERAGE INTENSITY DIFFERENCE FOR PATTERN" << endl
        << "GIVEN FOR BLOCKS OF 20 OBSERVATIONS." << endl;
    for (I=1; I <= datax->NPTS; I = I + 200)
    {
        for (J=1; J <= 10; ++J)
        {
            J20=20*(J-1);
            DEL[J]=0;
            for (K=1; K <= 20; ++K)
            {
                J20IK=J20+I+K-1;
                DEL[J]=DEL[J]+datax->Y[J20IK]-datax->BK[J20IK]-datax->YC[J20IK];
            }
            KI[J]=J+I/20;
            DEL[J]=DEL[J]/20.0;
            if (KI[J]*20 >= datax->NPTS) goto L15;
        }
        J=10;
L15:
        file6 << "     ";
        for (JJ=1; JJ <= J; ++JJ)
        {
            file6 << setw(4) << KI[JJ] << "  " << setw(5) << setprecision(1) << DEL[JJ];
        }
        file6 << endl;
    }
L37:
    for (I=1; I <= datax->NPTS; ++I)
    {
        datax->YC[I]=datax->YC[I]+datax->BK[I];
        if (cntrls->JOBTYP > 2) datax->Y[I]=datax->YC[I];
    }
    if (cntrls->IPC == 0) goto L36;
    //INQUIRE(UNIT=6,NAME=OUTFILE);
    //LBKSL = 0;
    //for (I=1; I <= 80; ++I) if ( OUTFILE[I] == "\\" ) LBKSL=I;
    //OUTFILELBKSL+1:) = 'plotinfo.bin';

    file9.open("plotinfo");
    file10.open("plotinfo.bin");
    for (I = 1; I <= datax->NPTS; ++I) file10 << datax->BK[I];
    file9 << char_->TITLE << endl;
    file9 << "NO. OF PHASESZ  " << cntrls->NPHASE << endl
        << "NO. OF REFLECTIONS IN EACH PHASEQ  ";
    for (IIPHAS=1; IIPHAS <= cntrls->NPHASE; ++IIPHAS) file9 << setw(4) << refls->ICR[IIPHAS];
    file9 << endl;
    file9 << "BRAGG POSITIONSZ" << endl;
    for (K=1; K <= cntrls->NPHASE; ++K)
    {
        for (I=1; I <= datax->NPTS; ++I) datax->KR[I] = 0;
        if ((K % 2) == 1) allp->ILOC = allp->ILOC + 1;
        file6 << "PHASE NO. = " << K << "     PHASE NAME " << char_->PHSNM[K] << endl;
        file9 << char_->PHSNM[K];
        T2OBS=0.0;
        TDOBS=0.0;
        RFNMR=0.0;
        RFDNR=0.0;
        ICZ=0;
        for (IIPHAS=1; IIPHAS <= cntrls->NPHASE; ++IIPHAS) ICZ = ICZ + refls->ICR[IIPHAS];
        IXX=0;
        IXXX=0;
        for (IX=1; IX <= ICZ; ++IX)
        {
            if( K != refls->IREFS[IX]/(256*256*256*8) )goto L481;
            SHIFT = params->GLB[10] * cos(refls->REFS[IX][2]/2.0/57.2958) + params->GLB[11] * sin(refls->REFS[IX][2]/57.2958);
            IXX=IXX+1;
            for (J=1; J <= jnk->NEXCRG; ++J)
            {
                //-----CHECK FOR SPLIT PEARSON VII PROFILE
                if (cntrls->NPROF == _SplitPearsonVII)
                {
                    if ((refls->REFS[IX][2]+SHIFT+params->GLB[1]) >= (jnk->ALOW[J]-g1->WDT*refls->FWHM[IX][1]) && (refls->REFS[IX][2]+SHIFT+params->GLB[1]) <= (jnk->AHIGH[J]+g1->WDT*refls->FWHM[IX][2]))  goto L481;
                    //-----FOR ALL OTHER PROFILES
                }
                else
                {
                    if ((refls->REFS[IX][2]+SHIFT+params->GLB[1]) >= (jnk->ALOW[J]-g1->WDT*refls->REFS[IX][1]) && (refls->REFS[IX][2]+SHIFT+params->GLB[1]) <= (jnk->AHIGH[J]+g1->WDT*refls->REFS[IX][1])) goto L481;
                }
            }
            IXXX=IXXX+1;                           //test !cp 29 jun 98
            IRL=(refls->IREFS[IX] % 256)-128;
            IRK=((refls->IREFS[IX]/256) % 256)-128;
            IRH=((refls->IREFS[IX]/(256*256)) % 256)-128;
            IRC=(refls->IREFS[IX]/(256*256*256)) % 8;
            //-----CHECK FOR THE SPLIT PEARSON VII PROFILE
            //-----if SO CHANGE THE PROFILE LIMITS
            if (cntrls->NPROF == _SplitPearsonVII)
            {
                RMIN=refls->REFS[IX][2]+params->GLB[1]+SHIFT-g1->WDT*refls->FWHM[IX][1];
                RMAX=refls->REFS[IX][2]+params->GLB[1]+SHIFT+g1->WDT*refls->FWHM[IX][2];
            }
            else
            {
                //-----FOR ALL OTHER PROFILES
                RMIN=refls->REFS[IX][2]+params->GLB[1]+SHIFT-g1->WDT*refls->REFS[IX][1];
                RMAX=refls->REFS[IX][2]+params->GLB[1]+SHIFT+g1->WDT*refls->REFS[IX][1];
            }
            MIN=static_cast<int>((RMIN-g1->THMIN)/g1->STEP+1.5);
            MAX=static_cast<int>((RMAX-g1->THMIN)/g1->STEP+1.5);
            MIN=max(MIN,1);
            MIN=min(MIN,datax->NPTS);
            MAX=min(MAX,datax->NPTS);
            //-----PATCH TO CALCULATE R-BRAGG
            prfx->TL=refls->REFS[IX][1];
            VERT=refls->REFS[IX][2] <= g1->RLIM;
            if (cntrls->NPROF == _pseudoVoigt)
            {
                prfx->GAM1=params->PAR[K][17] + params->PAR[K][18] * refls->REFS[IX][2];
            }
            else if (cntrls->NPROF == _PearsonVII)
            {
                prfx->GAM1=params->PAR[K][17]+(params->PAR[K][18]+params->PAR[K][19]/refls->REFS[IX][2])/refls->REFS[IX][2];
                PRSVII(prfx->GAM1);
            }
            else if (cntrls->NPROF == _TCHZ)
            {
                prfx->GAM1=refls->GAM[IX];
                TLG=refls->HALFG[IX];
                TLL=refls->HALFL[IX];
            }
            else if (cntrls->NPROF == _SplitPearsonVII)
            {
                spvii->RL=params->PAR[K][17]+(params->PAR[K][18]+params->PAR[K][19]/refls->REFS[IX][2])/refls->REFS[IX][2];
                spvii->RH=params->PAR[K][24]+(params->PAR[K][25]+params->PAR[K][26]/refls->REFS[IX][2])/refls->REFS[IX][2];
                mspvii(params->PAR[K][27],prfx->TL);
            }
            BB=prfx->TL*prfx->TL;
            TIOBS=0.0;
            TIC=0.0;
            refls->FMGNTD[IX] = refls->FMGNTD[IX]*refls->REFS[IX][3]*params->PAR[K][1];
            for (IS=MIN; IS <= MAX; ++IS)
            {
                TH=g1->THMIN+static_cast<double>(IS-1)*g1->STEP;
                if(jnk->NEXCRG > 0)
                {
                    for (IEXC=1; IEXC <= jnk->NEXCRG; ++IEXC)
                    {
                        if (TH >= jnk->ALOW[IEXC] && TH <= jnk->AHIGH[IEXC]) goto L410;
                    }
                }
                TH2 = 0.5*TH/RAD;
                prfx->DELTA=TH-refls->REFS[IX][2]-params->GLB[1]-SHIFT;
                prfx->DELT=prfx->DELTA*prfx->DELTA;
                //     NEXT LINE IS NECESSEARY FOR 2 PHASES WITH VERY DifFERENT FWHM.
                if (prfx->DELT/BB > g1->WDT*g1->WDT) goto L410;
                if( !VERT )goto L4;
                if (cntrls->IASYM == 0)
                {
                    YX=prfx->DELT * sign(prfx->DELTA);
                    Z=1.0-params->PAR[K][14]*YX/tan(TH2);
                }
                else
                {
                    YX=sign(prfx->DELTA)*prfx->DELTA/(2*prfx->TL);
                    if (TH2 > (45.0/RAD)) TH2 = TH2-(90.0/RAD);
                    Z=(params->PAR[K][14]/tan(TH2)) * (2.0*(prfx->DELTA/(2*prfx->TL))*exp(-YX));
                    Z=1+Z;
                }
                if(Z <= 0.0)Z=0.0001;
                goto L5;
L4:
                Z=1.0;
L5:
                OMEGA= Z*PROFIL(cntrls->NPROF,prfx->DELT/BB);
                if (cntrls->NPROF == _SplitPearsonVII)
                {
                    datax->KR[IS] = datax->KR[IS] + static_cast<int>(OMEGA*refls->FMGNTD[IX]);
                }
                else
                {
                    datax->KR[IS] = datax->KR[IS] + static_cast<int>(OMEGA*refls->FMGNTD[IX]/prfx->TL);
                }
                TIC   = TIC + OMEGA;
                TIOBS=TIOBS+OMEGA*(datax->Y[IS]-datax->BK[IS])/ max((datax->YC[IS]-datax->BK[IS]),1.0);
L410:;
            }
            TIC   = TIC   * refls->FMGNTD[IX]/prfx->TL;
            TIOBS = TIOBS * refls->FMGNTD[IX]/prfx->TL;
            // ! FROM ITALIAN CODE  !cp ap 97
            //        ***************************************************************
            //        NEXT LINE IS FOR NOT EVALUATING R-BRAGG FOR REFLECTIONS
            //        WHICH ARE OUTSIDE THE MEASUREMENTS RANGE BUT WHOSE TAILS ARE
            //        IN THE PATTERN
            //        ***************************************************************
            if((refls->REFS[IX][2]+SHIFT+params->GLB[1]) <= g1->THMAX)
            {
                T2OBS = T2OBS+TIOBS;
                TDOBS = TDOBS + abs(TIOBS-TIC);
                if (cntrls->IPC == 2 || cntrls->IPC == 3)
                {
                    TFCAL= sqrt(abs(TIC/refls->REFS[IX][3]/params->PAR[K][1]/struphase->TAVIX[IX]/struphase->SRIX[IX]));
                    TFOBS= sqrt(abs(TIOBS/refls->REFS[IX][3]/params->PAR[K][1]/struphase->TAVIX[IX]/struphase->SRIX[IX]));
                    AFCAL=TFCAL*cos(struphase->APHASE[IX]);
                    BFCAL=TFCAL*sin(struphase->APHASE[IX]);
                    AFOBS=TFOBS*cos(struphase->APHASE[IX]);
                    BFOBS=TFOBS*sin(struphase->APHASE[IX]);
                    //        Line below also from italian code   !cp ap 97
                    if(refls->REFS[IX][2] <= g1->THMAX)
                    {
                        RFNMR = RFNMR + abs(TFOBS-TFCAL);
                        RFDNR = RFDNR + abs(TFOBS);
                    }
                }
            }
            if (cntrls->NPROF == _TCHZ) goto L9222;
            if (cntrls->NPROF == _SplitPearsonVII) goto L9221;
            if( (IXXX-1 % 60) == 0)
            {
                // alterei aqui para criar arquivo com dados para determinacao de estruturas e graficos ! cp 22abril01
                if(cntrls->IPC == 1)
                {
                    file6 << "NO.  CODE    H   K   L   HW     POSN      ICALC     IOBS" << endl;
                    if(cntrls->IPL2 != 0) file69 << "   H   K   L   HW     POSN      ICALC       IOBS   Bragg_Pos" << endl;
                }
                if(cntrls->IPC == 2)
                {
                    file6 << "NO.  CODE     H   K   L    FWHM    POSN    ICALC      IOBS      FCALC     FOBS   PHASE_C" << endl;
                    if(cntrls->IPL2 != 0) file69 << "   H   K   L     FWHM    POSN    ICALC    IOBS      FCALC      FOBS   PHASE_C  Bragg_Pos" << endl;
                }
                if(cntrls->IPC == 3)
                {
                    file6 << "NO.  CODE     H   K   L     FWHM    POSN    ICALC      IOBS     A_CALC    B_CALC     A_OBS     B_OBS" << endl;
                    if(cntrls->IPL2 != 0) file69 << "   H   K   L     FWHM    POSN    ICALC       IOBS     A_CAL  B_CALC      A_OBS     B_OBS  Bragg_Pos" << endl;
                }
            }
            HW=refls->REFS[IX][1];
            if (cntrls->IPC == 1)
            {
                file6 << setw(4) << IXX
                    << setw(4) << IRC	<< "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(8) << setprecision(3) << HW
                    << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                    << setw(10) << setprecision(0) << TIC
                    << setw(10) << setprecision(0) << TIOBS << endl;
                if(IRC == 1)
                {
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << HW << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
            }
            else if (cntrls->IPC == 2)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC	<< "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(10) << setprecision(3) << TFCAL
                        << setw(10) << setprecision(3) << TFOBS
                        << setw(8) << setprecision(2) << RAD*struphase->APHASE[IX];
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << HW << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(1) << TIC << " "
                            << setw(8) << setprecision(1) << TIOBS << " "
                            << setw(8) << setprecision(1) << TFCAL << " "
                            << setw(8) << setprecision(1) << TFOBS << " "
                            << setw(9) << setprecision(4) << RAD*struphase->APHASE[IX] << " "
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC	<< "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS << endl;
                }
            }
            else if (cntrls->IPC == 3)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC	<< "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(10) << setprecision(3) << AFCAL
                        << setw(10) << setprecision(3) << BFCAL
                        << setw(10) << setprecision(3) << AFOBS
                        << setw(10) << setprecision(3) << BFOBS << endl;
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << HW << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(1) << TIC << " "
                            << setw(8) << setprecision(1) << TIOBS << " "
                            << setw(8) << setprecision(1) << AFCAL << " "
                            << setw(8) << setprecision(1) << BFCAL << " "
                            << setw(9) << setprecision(3) << AFOBS << " "
                            << setw(9) << setprecision(3) << BFOBS << " "
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC	<< "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS << endl;
                }
            }
            file9 << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                << " K" << setw(1) << IRC << " "
                << setw(4) << IRH
                << setw(4) << IRK
                << setw(4) << IRL
                << setw(10) << setprecision(0) << refls->FMGNTD[IX] << endl;
            goto L481;
L9221:
            if( (IXXX-1 % 60) == 0)
            {
                if(cntrls->IPC == 1)
                {
                    file6 << "NO.  CODE    H   K   L      HWL     HWH  POSN       ICALC     IOBS  PHASE" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L      HWL       HWH      POSN     ICALC      IOBS    PHASE  Bragg_Pos" << endl;
                }
                if(cntrls->IPC == 2)
                {
                    file6 << "NO.  CODE    H   K   L      HWL     HWH  POSN      ICALC     IOBS        FCALC    FOBS       PHASE_C" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L      HWL       HWH      POSN     ICALC      IOBS    FCALC     FOBS     PHASE_C  Bragg_Pos" << endl;
                }
                if(cntrls->IPC == 3)
                {
                    file6 << "NO.  CODE    H   K   L      HWL     HWH  POSN      ICALC     IOBS        A_CALC     B_CALC     A_OBS     B_OBS" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L      HWL      HWH      POSN     ICALC      IOBS   A_CALC   B_CALC   A_OBS    B_OBS  Bragg_Pos" << endl;
                }
            }
            if(cntrls->IPC == 1)
            {
                file6 << setw(4) << IXX
                    << setw(4) << IRC << "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(8) << setprecision(3) << refls->FWHM[IX][1]
                    << setw(8) << setprecision(3) << refls->FWHM[IX][2]
                    << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                    << setw(10) << setprecision(3) << TIC
                    << setw(10) << setprecision(3) << TIOBS << endl;
                if(IRC == 1)
                {
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][1] << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][2] << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(8) << setprecision(4) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
            }
            else if(cntrls->IPC == 2)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << refls->FWHM[IX][1]
                        << setw(8) << setprecision(3) << refls->FWHM[IX][2]
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(10) << setprecision(3) << TFCAL
                        << setw(10) << setprecision(3) << TFOBS << " "
                        << setw(8) << setprecision(2) << RAD*struphase->APHASE[IX] << endl;
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][1] << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][2] << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(8) << setprecision(4) << TFCAL << " "
                            << setw(8) << setprecision(4) << TFOBS << " "
                            << setw(9) << setprecision(4) << RAD*struphase->APHASE[IX]
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << refls->FWHM[IX][1]
                        << setw(8) << setprecision(3) << refls->FWHM[IX][2]
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC << endl;
                }
            }
            else if(cntrls->IPC == 3)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << refls->FWHM[IX][1]
                        << setw(8) << setprecision(3) << refls->FWHM[IX][2]
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(10) << setprecision(3) << AFCAL
                        << setw(10) << setprecision(3) << BFCAL
                        << setw(10) << setprecision(3) << AFOBS
                        << setw(10) << setprecision(3) << BFOBS << endl;
                    if (cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][1] << " "
                            << setw(8) << setprecision(4) << refls->FWHM[IX][2] << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(8) << setprecision(2) << AFCAL << " "
                            << setw(8) << setprecision(2) << BFCAL << " "
                            << setw(8) << setprecision(2) << AFOBS
                            << setw(8) << setprecision(2) << BFOBS
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << refls->FWHM[IX][1]
                        << setw(8) << setprecision(3) << refls->FWHM[IX][2]
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS << endl;
                }
            }
            file9 << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " K"
                << setw(1) << IRC << " "
                << setw(4) << IRH
                << setw(4) << IRK
                << setw(4) << IRL
                << setw(10) << setprecision(4) << refls->FMGNTD[IX] << endl;
            goto L481;
L9222:
            if((IXXX-1 % 60) == 0)
            {
                if (cntrls->IPC == 1)
                {
                    file6 << "NO. CODE     H   K   L     HW     POSN     ICALC    IOBS       HG      HL      ETA   PHASE_C" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L     HW      POSN     ICALC    IOBS    HGHL      ETA   PHASE_C" << endl;
                }
                if (cntrls->IPC == 2)
                {
                    file6 << "NO. CODE     H   K   L     HW     POSN    ICALC      IOBS      HG      HL      ETA     FCALC     FOBS   PHASE_C" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L     HW       POSN    ICALC    IOBS    HG     HL      ETA   FCALC     FOBS   PHASE_C" << endl;
                }
                if (cntrls->IPC == 3)
                {
                    file6 << "NO. CODE     H   K   L     HW     POSN    ICALC      IOBS      HG      HL      ETA    A_CALC    B_CALC     A_OBS     B_OBS" << endl;
                    if(cntrls->IPL2 != 0) file69 << "  H   K   L     HW       POSN    ICALC    IOBS    HG     HL      ETA   A_CALC    B_CALC     A_OBS     B_OBS" << endl;
                }
            }
            HW=refls->REFS[IX][1];
            if (cntrls->IPC == 1)
            {
                file6 << setw(4) << IXX
                    << setw(4) << IRC << "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(8) << setprecision(3) << HW
                    << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                    << setw(10) << setprecision(0) << TIC
                    << setw(10) << setprecision(0) << TIOBS
                    << setw(8) << setprecision(3) << TLG
                    << setw(8) << setprecision(3) << TLL
                    << setw(8) << setprecision(3) << prfx->GAM1
                    << setw(10) << setprecision(3) << RAD*struphase->APHASE[IX] << endl;
                if(cntrls->IPL2 != 0)
                {
                    file69 << setw(3) << IRH << " "
                        << setw(3) << IRK << " "
                        << setw(3) << IRL << " "
                        << setw(8) << setprecision(4) << HW << " "
                        << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                        << setw(8) << setprecision(0) << TIC << " "
                        << setw(8) << setprecision(4) << TIOBS << " "
                        << setw(8) << setprecision(4) << TLG << " "
                        << setw(8) << setprecision(4) << TLL << " "
                        << setw(8) << setprecision(4) << prfx->GAM1 << " "
                        << setw(9) << setprecision(4) << RAD*struphase->APHASE[IX]
                        << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                }
            }
            else if(cntrls->IPC == 2)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(8) << setprecision(3) << TLG
                        << setw(8) << setprecision(3) << TLL
                        << setw(8) << setprecision(3) << prfx->GAM1
                        << setw(10) << setprecision(3) << TFCAL
                        << setw(10) << setprecision(3) << TFOBS << " "
                        << setw(8) << setprecision(2) << struphase->APHASE[IX] << endl;
                    if(cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << HW << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(8) << setprecision(4) << TLG << " "
                            << setw(8) << setprecision(4) << TLL << " "
                            << setw(8) << setprecision(4) << prfx->GAM1 << " "
                            << setw(8) << setprecision(4) << TFCAL << " "
                            << setw(8) << setprecision(4) << TFOBS << " "
                            << setw(9) << setprecision(4) << struphase->APHASE[IX] << " "
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(8) << setprecision(3) << TLG
                        << setw(8) << setprecision(3) << TLL
                        << setw(8) << setprecision(3) << prfx->GAM1 << endl;
                }
            }
            else if(cntrls->IPC == 3)
            {
                if (IRC == 1)
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(8) << setprecision(3) << TLG
                        << setw(8) << setprecision(3) << TLL
                        << setw(8) << setprecision(3) << prfx->GAM1
                        << setw(10) << setprecision(4) << AFCAL
                        << setw(10) << setprecision(4) << BFCAL
                        << setw(10) << setprecision(4) << AFOBS
                        << setw(10) << setprecision(4) << BFOBS << endl;
                    if(cntrls->IPL2 != 0)
                    {
                        file69 << setw(3) << IRH << " "
                            << setw(3) << IRK << " "
                            << setw(3) << IRL << " "
                            << setw(8) << setprecision(4) << HW << " "
                            << setw(8) << setprecision(4) << refls->REFS[IX][2]+params->GLB[1]+SHIFT << " "
                            << setw(8) << setprecision(0) << TIC << " "
                            << setw(8) << setprecision(0) << TIOBS << " "
                            << setw(8) << setprecision(4) << TLG << " "
                            << setw(8) << setprecision(4) << TLL << " "
                            << setw(8) << setprecision(4) << prfx->GAM1 << " "
                            << setw(8) << setprecision(4) << AFCAL << " "
                            << setw(8) << setprecision(4) << BFCAL << " "
                            << setw(8) << setprecision(4) << AFOBS << " "
                            << setw(8) << setprecision(4) << BFOBS << " "
                            << setw(9) << setprecision(0) << (-K*maxint->XMAXINT/10) << endl;
                    }
                }
                else
                {
                    file6 << setw(4) << IXX
                        << setw(4) << IRC << "   "
                        << setw(4) << IRH
                        << setw(4) << IRK
                        << setw(4) << IRL
                        << setw(8) << setprecision(3) << HW
                        << setw(8) << setprecision(3) << refls->REFS[IX][2]+params->GLB[1]+SHIFT
                        << setw(10) << setprecision(0) << TIC
                        << setw(10) << setprecision(0) << TIOBS
                        << setw(8) << setprecision(3) << TLG
                        << setw(8) << setprecision(3) << TLL
                        << setw(8) << setprecision(3) << prfx->GAM1 << endl;
                }
            }
L481:;
        }
        // version II - format for compatibility with Sakthevil's PLOT program
        for (I=1; I <= datax->NPTS; ++I) file10 << datax->KR[I];
        TDOBS=100.0*TDOBS/T2OBS;
        file6 << "DERIVED BRAGG R-FACTOR = " << setw(8) << setprecision(2) << TDOBS << endl;
        if (cntrls->IPC != 1) file6 << "DERIVED R-F            = " << setw(8) << setprecision(2) << 100.0*RFNMR/RFDNR  << endl;
        allp->FINAL[allp->ILOC][2-(K % 2)] = TDOBS;
        if (sizestrain->NSIZESTRAIN  ==  9) size(K);
        //L400:
    }
L36:
    file9
        << " NPTSZ" << setw(5) << datax->NPTS << endl
        << " THMINZ" << setw(8) << setprecision(4) << g1->THMIN << endl
        << " STEPZ" << setw(8) << setprecision(4) << g1->STEP << endl
        << " YOBS    YCALCZ" << endl;
    for (I=1; I <= datax->NPTS; ++I)
    {
        datax->BK[I]=g1->THMIN+static_cast<double>(I-1)*g1->STEP;
        file9 << " "
            << setw(8) << setprecision(0) << datax->Y[I]
        << setw(8) << setprecision(0) << datax->YC[I] << endl;
        if(cntrls->IPL2 != 0)
        {
            file690 << " " << setw(9) << setprecision(4) << g1->THMIN+static_cast<double>(I-1)*g1->STEP
                << " " << setw(9) << setprecision(0) << datax->Y[I]
            << " " << setw(9) << setprecision(0) << datax->YC[I]
            << " " << setw(9) << setprecision(0) << datax->Y[I]-datax->YC[I]
            << " " << setw(9) << setprecision(0) << ( pow(datax->Y[I]-datax->YC[I],2) )/datax->Y[I]
            << endl;
        }
    }
    file9.close();
    file10.close();
    if(cntrls->IPL2 != 0)
    {
        file69.close();
        file690.close();
    }
    //     IT BUILDS THE OBSERVED DATA FILE CORRECTED FOR ABSORPTION
    if(cntrls->IPLOSS == 1 && cntrls->IPBIG == 0)
    {
        file31.open("PLOTOSS.COR");
        file31 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE1 << endl;
        file31 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file31 << setw(15) << setprecision(5) << datax->Y[I] << endl;
        }
        file31.close();
    }
    //     IT BUILDS THE CALCULATED DATA FILE
    //     (BRAGG+COMPTON+DISORDINE+AMORPHOUS)
    if(cntrls->IPLCAL == 1 && cntrls->IPBIG == 0)
    {
        file32.open("PLOTCAL.TOT");
        file32 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE2 << endl;
        file32 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file32 << setw(15) << setprecision(5) << datax->YC[I] << endl;
        }
        file32.close();
    }
    //     IT BUILDS THE TOTAL COMPTON SCATTERING FILE
    //     FOR ALL PHASES
    if(cntrls->IPLCOM == 1 && cntrls->IPBIG == 0)
    {
        file33.open("PLOTCOM.TOT");
        file33 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE3 << endl;
        file33 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file33 << setw(15) << setprecision(5) << fondi->BKCOM[I] << endl;
        }
        file33.close();
    }
    //     IT BUILDS THE TOTAL DISORDER SCATTERING FILE
    //     FOR ALL PHASES
    if(cntrls->IPLDIS == 1 && cntrls->IPBIG == 0)
    {
        file34.open("PLOTDIS.TOT");
        file34 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE4 << endl;
        file34 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file34 << setw(15) << setprecision(5) << fondi->BKDIS[I] << endl;
        }
        file34.close();
    }
    //     IT BUILDS THE POLYNOMIAL BACKGROUND FILE
    if(cntrls->IPLPOL == 1 && cntrls->IPBIG == 0)
    {
        file37.open("PLOTPOL.TOT");
        file37 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE7 << endl;
        file37 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file37 << setw(15) << setprecision(5) << fondi->BKPOL[I] << endl;
        }
        file37.close();
    }
    //     IT BUILDS THE AMORPHOUS FILE
    if(cntrls->IPLAM == 1 && cntrls->IPBIG == 0)
    {
        file36.open("PLOTAM.TOT");
        file36 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE6 << endl;
        file36 << setw(6) << datax->NPTS
            << setw(15) << setprecision(5) << g1->STEP
            << setw(15) << setprecision(5) << g1->THMIN
            << setw(15) << setprecision(5) << g1->THMAX
            << setw(5) << 1 << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            file36 << setw(15) << setprecision(5) << fondi->BKAM[I] << endl;
        }
        file36.close();
    }
    //     IT BUILDS THE TOTAL PLOT FILE
    if(cntrls->IPBIG == 1)
    {
        file38.open("PLOTBIG.DAT");
        file38 << " \"" << setw(70) << char_->TITLE << setw(8) << TITLE8 << endl;

        file38 << "       ANG       OSS       CAL       +AM      +POL      +DIS      +COM       RES" << endl;
        for (I=1; I <= datax->NPTS; ++I)
        {
            XXXX=g1->THMIN+static_cast<double>(I-1)*g1->STEP;
            file38 << setw(10) << setprecision(3) << XXXX << ","
                << setw(10) << setprecision(3) << datax->Y[I] << ","
                << setw(10) << setprecision(3) << datax->YC[I] << ","
                << setw(10) << setprecision(3) << fondi->BKAM[I] << ","
                << setw(10) << setprecision(3) << fondi->BKAM[I]+fondi->BKPOL[I] << ","
                << setw(10) << setprecision(3) << fondi->BKAM[I]+fondi->BKPOL[I]+fondi->BKDIS[I] << ","
                << setw(10) << setprecision(3) << fondi->BKAM[I]+fondi->BKPOL[I]+fondi->BKDIS[I]+fondi->BKCOM[I] << ","
                << setw(10) << setprecision(3) << pow(datax->Y[I]-datax->YC[I],2)/datax->Y[I]
            << endl;
        }
        file38.close();
    }
    //L12045:
    if (cntrls->IOT == 0) goto L30;
    NP=datax->NPTS/240;
    //xxxxxxxxxxxxxxxxxxxxxxx
    for (N=1; N <= NP; ++N)
    {
        for (I=1; I <= 4; ++I) file6 << "2THETA   YOBS    YCALC VARIANCE ";
        file6 << endl;
        for (J=1; J <= 60; ++J)
        {
            for (I=1; I <= 4; ++I)
            {
                file6 << setw(7) << setprecision(3) << datax->BK[240*N-300+60*I+J]
                << setw(8) << setprecision(0) << datax->Y[240*N-300+60*I+J]
                << setw(8) << setprecision(0) << datax->YC[240*N-300+60*I+J]
                << setw(8) << setprecision(0) << datax->VAR[240*N-300+60*I+J];
            }
            file6 << endl;
        }
    }
    NPTS2=datax->NPTS-NP*240;
    if (NPTS2 == 0) goto L30;
    NCOL=NPTS2/60;
    for (I=1; I <= 4; ++I) file6 << "2THETA   YOBS    YCALC VARIANCE ";
    NLINES=NPTS2-NCOL*60;
    if (NLINES == 0) goto L33;
    NCOL1=NCOL+1;
    NP=240*NP-60;
    for (J=1; J <= NLINES; ++J)
    {
        for (I=1; I <= NCOL1; ++I)
        {
            file6 << setw(7) << setprecision(3) << datax->BK[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->Y[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->YC[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->VAR[NP+60*I+J];
        }
        file6 << endl;
    }
    NP=NP+NLINES;
    NLINES=60-NLINES;
L33:
    for (J=1; J <= NLINES; ++J)
    {
        for (I=1; I <= NCOL1; ++I)
        {
            file6 << setw(7) << setprecision(3) << datax->BK[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->Y[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->YC[NP+60*I+J]
            << setw(8) << setprecision(0) << datax->VAR[NP+60*I+J];
        }
        file6 << endl;
    }
L30:
    if (cntrls->IPL == 0) goto L45;
    for (I=1; I <= 12; ++I) LABEL[I]=10*I*ISCALE;
    file6 << "                              " << char_->TITLE << endl << endl;
    for (I=1; I <= 12; ++I) file6 << setw(10) << LABEL[I];
    file6 << endl;
    for (I=1; I <= 12; ++I) LABEL[I]=-60*IDIF+10*I*IDIF;
    for (I=1; I <= 12; ++I) file6 << setw(10) << LABEL[I];
    for (I=1; I <= datax->NPTS; ++I)
    {
        //L1:
        IOUT = " ";
        for (J=1; J <= 120; ++J) IOUT=IOUT+ISPACE;
        IOUT[1]=IDOT;
        IOUT[61]=IDOT;
        IOUT[120]=IDOT;
        //L200:
        IY=static_cast<int>(datax->Y[I])/ISCALE+2;
        IY=max(min(IY,120),2);
        IOUT[IY-1]=IBAR;
        IOUT[IY]=IPLUS;
        if(IY <= 119) IOUT[IY+1]=IBAR;
        IY=static_cast<int>(datax->YC[I])/ISCALE+2;
        IY=max(min(IY,120),2);
        IOUT[IY]=IMINUS;
        IY=static_cast<int>(datax->Y[I]-datax->YC[I])/IDIF+61;
        IY=max(min(IY,120),2);
        IOUT[IY]=ISTAR;
        if((I-1 % 10) != 0) goto L55;
        TLABEL=g1->THMIN+g1->STEP*static_cast<double>(I-1);
        for (J=1; J <= 113; ++J) file6 << IOUT[J];
        file6 << setw(6) << setprecision(2) << TLABEL;
        file6 << IOUT[120] << endl;
        goto L60;
L55:
        for (J=1; J <= 120; ++J) file6 << IOUT[J];
        file6 << endl;
L60:;
    }
L45:
    if (cntrls->JOBTYP < 3) goto L72;

    file4.close();
    file4b.open(file4name.data(),ios::trunc);			//  REWIND 4;
    file4b << setw(8) << setprecision(4) << g1->THMIN
        << setw(8) << setprecision(4) << g1->STEP
        << setw(8) << setprecision(4) << g1->THMAX
        << endl;
    J = 0;
    for (I=1; I <= datax->NPTS; ++I)
    {
        ++J;
        file4b << setw(7) << setprecision(0) << " " << datax->YC[I];
        if (J == 8)
        {
            file4b << endl;
            J = 0;
        }
    }
    if (file4b.is_open()) file4b.close();
L72:
    if (cntrls->IPLST != 0 && cntrls->MAXS != 0)
    {
        INUMB = cntrls->ICYRUN + 1;
        NPAGES = cntrls->MAXS/12;
        if ((cntrls->MAXS % 12) != 0) NPAGES = NPAGES+1;
        file6 << "PARAMETERS IN EACH CYCLE" << endl;

        if (file8o.is_open()) file8o.close();

        //     LIST PARAMETERS IN EACH CYCLE
        for (J=1; J <= NPAGES; ++J)
        {
            file8i.open(file8name.data());			// REWIND 8;
            ISTART = 1 + (J-1)*12;
            IFINIS  = min(cntrls->MAXS,12 + (J-1)*12);
            file6 << "CYCLE";
            for (L=ISTART; L <= IFINIS; ++L) file6 << "   " << setw(2) << L << "     ";
            file6 << endl;
            for (K=1; K <= INUMB; ++K)
            {
                for (I=1; I <= 2*cntrls->MAXS+4; ++I) file8i >> DUMMY[I];
                file6 << setw(2) << K-1 << ")  ";
                file6 << scientific;
                for (I=ISTART; I <= IFINIS; ++I) file6 <<  setw(10) << setprecision(4) << DUMMY[I] << " ";
                file6 << fixed << endl;
            }
            file8i.close();
        }
        //     LIST R-VALUES  IN EACH CYCLE
        file8i.open(file8name.data());			// REWIND 8;
        file6 << "R-VALUE VARIATION WITH CYCLE" << endl
            << "CYCLE    R-P      R-WP      S      D-W D" << endl;
        for (K=1; K <= INUMB; ++K)
        {
            for (I=1; I <= 2*cntrls->MAXS+4; ++I) file8i >> DUMMY[I];
            file6 << setw(2) << K-1 << ")  ";
            for (I=2*cntrls->MAXS+1; I <= 2*cntrls->MAXS+4; ++I) file6 << setw(8) << setprecision(2) << DUMMY[I] << " ";
            file6 << endl;
        }
        file8i.close();

        //C TESTE PARA GERAR SAIDA PARA 9411
        if(cntrls->I2D94 != 0)
        {
            WRITE94(ISCALE,IDIF);
        }
        //     LIST PARAMETER SHIFTS IN EACH CYCLE
        if (INUMB == 1) goto L88880;
        file6 << "APPLIED PARAMETER SHIFT IN EACH CYCLE" << endl;
        for (J=1; J <= NPAGES; ++J)
        {
            file8i.open(file8name.data());			// REWIND 8;
            ISTART = 1 + cntrls->MAXS+ (J-1)*12;
            IFINIS = min(2*cntrls->MAXS,12 + cntrls->MAXS+ (J-1)*12);
            file6 << "CYCLE";
            for (L=ISTART-cntrls->MAXS; L <= IFINIS-cntrls->MAXS; ++L) file6 << "   " << setw(2) << "     ";
            file6 << endl;
            for (K=1; K <= INUMB-1; ++K)
            {
                for (I=1; I <= 2*cntrls->MAXS+4; ++I) file8i >> DUMMY[I];
                if (K == 1) goto L7964;
L7964:
                file6 << setw(2) << K << ")  ";
                file6 << scientific;
                for (I=ISTART; I <= IFINIS; ++I) file6 << setw(10) << setprecision(4) << DUMMY[I] << " ";
                file6 << fixed << endl;
            }
        }
        file8i.close();

        file8o.open(file8name.data());

    }
L88880:
    //     CODE FOR PRINTING PARAMETERS AND STD. DEV. IN THE FINAL CYCLE
    if (cntrls->IPLST == 2 && cntrls->MAXS != 0)
    {
        file6 << "PARAMETERS AND STANDARD DEVIATIONS IN THE FINAL CYCLE FOR DATA BASE" << endl
            << " \" " << char_->TITLE << " \" " << endl;
        file6 << scientific << setw(10) << setprecision(4);
        for (I=1; I <= allp->ILOC; ++I)
        {
            file6 << allp->FINAL[I][1] << allp->FINAL[I][2];
        }
        file6 << fixed << endl;
    }
    return;
    //L99990:
    //	DBWSException("ERROR IN WRITING TO FILE 6 IN SUBROUTINE EXPUT");
    //L99991:
    //	DBWSException("ERROR WRITING PARAMS,ST.DEV. IN SUBROUTINE EXPUT");
    //L99992:
    //	DBWSException("ERROR IN READING FROM UNIT 8 IN SUBROUTINE EXPUT");
}

void DBWS::REFGEN(int IPHASE, double ZERO, double DIS, double TRANS, double PREF[][3+1], double PREFOR)
{
    const string LAU[14 +1] = {"",  "1BAR","2/M","MMM","4/M","4/MMM","3BAR   R","3BAR M R","3BAR","3BAR M 1","3BAR 1 M","6/M","6/MMM","M3","M3M"};
    int I, H1, H2, H3, I1, L1, I2, I3, IC,I1D, I2D, I3D,LX1, I12D, I13D, I23D,
        LXN,I123D,KXIS,I2DEL, I3DEL, I1MAX, I2MAX, I3MAX,IORDR1, IORDR2,IORDR3,IIPHAS;
    double ANGTTETA,SQ,RAD, SQH, POS,TLR,TAN2,XABS, TMIN, SMAX,TANX, PLOR,SMAX1,
        SHIFT,THMAX1,THMAXX;
    bool ORH1, ORH2, ORH3;

    POS = 0.0;

    RAD = 3.14159265359/360.0;
    if ( spgcom->NAXIS > 3 ) DBWSException("5001");
    KXIS = spgcom->NAXIS;
    spgcom->NAXIS = 1;
    //L1002:
    //J = 0;
    I2D = 0;
    I3D = 0;
    I1D = 1;
    I12D = 1;
    I13D = 1;
    I123D = 1;
    I23D = 1;
    I2DEL = 1;
    I3DEL = 1;
    for (I=1; I <= 8; ++I)
    {
        if ( spgcom->NCONT[I] == 0 ) goto L9000;
        if ( spgcom->NCONT[I]-8192 < 0 )
        {
            goto L8002;
        }
        else if ( spgcom->NCONT[I]-8192 == 0 )
        {
            goto L8001;
        }
        else
        {
            goto L8000;
        }
L8001:
        I3DEL = 3;
        I1D = 2;
        I123D = 3;
        goto L9000;
L8002:
        if ( spgcom->NCONT[I]-576 == 0 ) goto L8003; else goto L8004;
L8003:
        I3DEL = 2;
        I23D = 2;
        goto L8000;
L8004:
        if ( spgcom->NCONT[I]-516 == 0 ) goto L8009; else goto L8008;
L8009:
        I3DEL = 2;
        I13D = 2;
        goto L8000;
L8008:
        if ( spgcom->NCONT[I]-68 == 0 ) goto L8013; else goto L8012;
L8012:
        if ( spgcom->NCONT[I]-580  == 0) goto L8016; else goto L8015;
L8013:
        I2DEL = 2;
        I12D = 2;
        goto L8000;
L8016:
        I123D = 2;
        I3DEL = 2;
L8015:;
L8000:;
    }
L9000:
    //	N = J-1;
    //L97:
    TMIN = pow(( sin( (g1->THMIN/720.0)*6.28318531 )/g1->LAMDA[1]),2);
    IC = 0;
    if ( IPHASE >= 2 )
    {
        for (IIPHAS=2; IIPHAS <= IPHASE; ++IIPHAS) IC = IC+refls->ICR[IIPHAS-1];
    }
    SMAX = pow(( sin((g1->THMAX/720.0)*6.28318531) /g1->LAMDA[1]),2);

    //     **************************************************************
    //     SMAX IS CHANGED TO ACCOUNT FOR THE LEFT TAILS OF THE REFLECTIONS
    //     THAT ARE PRESENT AT ANGLES GREATER THAN THMAX
    //     **************************************************************
    //    PRINT *,'THMAX=',THMAX
    //      THMAX1=(U*(TAN(THMAX*RAD))**2+V*TAN(THMAX*RAD)+W+ZZZ*(1+
    //     *(TAN(THMAX*RAD))**2))
    // also incorporating the cotg^2 term  !cp may 01 97

    THMAX1 = (g1->U * pow( (tan(g1->THMAX*RAD)),2) +
              g1->V * tan(g1->THMAX*RAD)+
              g1->W+
              g1->ZZZ*
              (1+ pow( (tan(g1->THMAX*RAD)),2) ) +
              g1->UC/(tan(g1->THMAX*RAD)));
    if ( THMAX1 > 0.0 )
    {
        THMAX1= g1->WDT*sqrt(THMAX1)/2;
    }
    else
    {
        cout << "2" << endl;
        file6 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << " PHASE NO. " << setw(4) << IPHASE << endl;
        file7 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << " PHASE NO. " << setw(4) << IPHASE << endl;
        DBWSException("SQUARE OF FWHM IS NEGATIVE");
    }
    ANGTTETA=g1->THMAX+THMAX1;
    if ( (g1->THMAX+THMAX1) >= 180.0 ) ANGTTETA=180.0;

    // TODO: mudar para ANGTTETA/2 * 2*pi/360
    SMAX1 = pow(( sin((ANGTTETA/720.0)*6.28318531)/g1->LAMDA[1]),2);
    SMAX = SMAX1;
    THMAXX=ANGTTETA;
    if ( g1->LAMDA[2] > g1->LAMDA[1] ) TMIN=TMIN* pow( (g1->LAMDA[1]/g1->LAMDA[2]) , 2);
    if ( g1->LAMDA[2] > 0.0  &&  g1->LAMDA[2] < g1->LAMDA[1] )SMAX=SMAX* pow((g1->LAMDA[1]/g1->LAMDA[2]) ,2);
    OP1(&IPHASE);
    file6
        << "LAUE SYMMETRY " << LAU[spgcom->NSPGRP] << " WILL BE USED TO GENERATE INDICES" << endl
        << "       -------------------------------------------" << endl;
    I1MAX=static_cast<int>(cellx->A*2.0*sqrt(SMAX));
    if ( KXIS < 3 )
    {
        I2MAX=static_cast<int>(cellx->B*2.0*sqrt(SMAX));
        I3MAX=static_cast<int>(cellx->C*2.0*sqrt(SMAX));
    }
    else
    {
        I2MAX = static_cast<int>(2.0*cellx->C*sqrt(SMAX));
        I3MAX = static_cast<int>(2.0*cellx->B*sqrt(SMAX));
    }
    L1 = spgcom->NSPGRP;
    if ( L1 >= 13 && spgcom->NCONT[1] == 580 ) L1=L1+2;
    if ( I12D+I13D+I23D != 5 ) goto L2131;
    I12D = 2;
    I13D = 1;
    I23D = 2;
    I2DEL = 2;
    I3DEL = 2;
L2131:
    if ( L1 == 6 ) I2D=1;
    if ( L1 == 7 ) I2D=2;
    I3D = I2D;
    I1 = -1;
L2200:
    I1 = 1+I1;
    if ( I1-I1MAX <= 0 ) goto L2201; else goto L2303;
L2201:
    H1 = I1;
    if ( I2D > 0 ) I2MAX=I1;
    switch (L1) {
    case 1:
        goto L2203;
        break;
    case 2:
        goto L2204;
        break;
    case 3:
        goto L2204;
        break;
    case 4:
        goto L2205;
        break;
    case 5:
        goto L2206;
        break;
    case 6:
        goto L2208;
        break;
    case 7:
        goto L2209;
        break;
    case 8:
        goto L2207;
        break;
    case 9:
        goto L2204;
        break;
    case 10:
        goto L2206;
        break;
    case 11:
        goto L2204;
        break;
    case 12:
        goto L2206;
        break;
    case 13:
        goto L2206;
        break;
    case 14:
        goto L2206;
        break;
    case 15:
        goto L2206;
        break;
    case 16:
        goto L2206;
        break;
    }
    GOTOER();
L2203:
    I2= -min(I2MAX,I1*I2MAX);
    goto L2210;
L2204:
    I2 = 0;
    goto L2210;
L2205:
    I2= min(I1,1);
    goto L2210;
L2206:
    I2 = I1;
    goto L2210;
L2207:
    I2= min(-I1+1,0);
    goto L2210;
L2208:
    I2 = -2*I1;
    goto L2210;
L2209:
    I2 = -I1/2;
L2210:
    I2= I2DEL*(I2/I2DEL)+(I1 % I12D);
    goto L2221;
L2220:
    I2 = I2DEL+I2;
L2221:
    H2 = I2;
    if ( I2-I2MAX <= 0 ) goto L2222; else goto L2200;
L2222:
    switch (L1) {
    case 1:
        goto L2223;
        break;
    case 2:
        goto L2223;
        break;
    case 3:
        goto L2224;
        break;
    case 4:
        goto L2224;
        break;
    case 5:
        goto L2224;
        break;
    case 6:
        goto L2225;
        break;
    case 7:
        goto L2225;
        break;
    case 8:
        goto L2224;
        break;
    case 9:
        goto L2224;
        break;
    case 10:
        goto L2223;
        break;
    case 11:
        goto L2224;
        break;
    case 12:
        goto L2224;
        break;
    case 13:
        goto L2226;
        break;
    case 14:
        goto L2227;
        break;
    case 15:
        goto L2236;
        break;
    case 16:
        goto L2235;
        break;
    }
    GOTOER();
L2223:
    I3= -min(I3MAX,int((I1+abs(I2))*I3MAX)); //xxxxxxxxxxxxxxxxxxxxxxxxx
    goto L2228;
L2224:
    I3 = 0;
    goto L2228;
L2225:
    I3 = -I1-I2;
    goto L2228;
L2226:  I3= min(I2,I1+I3DEL);
    goto L2228;
L2227:
    I3 = I2;
L2228:  I3= I3DEL*(I3/I3DEL)+  (((I1+I1D*I2 % I123D)+I123D) % I123D) + (I1 % I13D)+ (I2 % I23D);
    if ( I3D-1 < 0 )
    {
        goto L2232;
    }
    else if ( I3D-1 == 0 )
    {
        goto L2229;
    }
    else
    {
        goto L2231;
    }
L2229:
    I3MAX = I1;
    if ( I2-I1 == 0 ) goto L2232; else goto L2230;
L2230:
    I3MAX = I3MAX-1;
    goto L2232;
L2231:
    I3MAX = I2;
    goto L2232;
L2235:
    I3= I2+(I1 % 2);
    goto L2232;
L2236:
    I3 = I1+2-(I2 % 2);
    if ( I1 == I2 && (I1 % 2) == 0 ) I3=I1;
    goto L2232;
L2233:
    I3 = I3DEL+I3;
L2232:
    H3 = I3;
    if ( KXIS != 3 ) goto L2240;
    H3 = I2;
    H2 = I3;
L2240:;
    if ( I3-I3MAX <= 0 ) goto L113; else goto L2220;
L113:
    SQ = static_cast<double>(H1*H1)*cellx->AL[1][1]+
         static_cast<double>(H2*H2)*cellx->AL[2][2]+
         static_cast<double>(H3*H3)*cellx->AL[3][3]+
         static_cast<double>(H1*H2)*cellx->AL[1][2]+
         static_cast<double>(H1*H3)*cellx->AL[1][3]+
         static_cast<double>(H2*H3)*cellx->AL[2][3];
    SQ=SQ/4.0;
    //L2234:
    if ( SQ-SMAX <= 0 ) goto L3000; else goto L2233;
L2303:
    refls->ICR[IPHASE]=IC;
    if (IPHASE >= 2)
    {
        for (IIPHAS=2; IIPHAS <= IPHASE; ++IIPHAS) refls->ICR[IPHASE] = refls->ICR[IPHASE]-refls->ICR[IIPHAS-1];
    }
    SORT(IPHASE);
    return;
L3000:
    if ( SQ - TMIN < 0 ) goto L2233; else goto L3117;
L3117:
    if ( IC > IRS-2 ) goto L6001;
    //     NEXT if BLOCK FOR PHASE WITH DISTINCT ORIENTATION ALONG PREF(NAXIS,I)
    if (PREFOR > 99.0)
    {
        ORH1 = static_cast<int>(PREF[IPHASE][1]) == H1;
        if ( !(ORH1) && H1 != 0 && static_cast<int>(PREF[IPHASE][1]) != 0) ORH1 = (H1 % static_cast<int>(PREF[IPHASE][1])) == 0;
        if ( !ORH1 ) goto L2233;

        ORH2 = static_cast<int>(PREF[IPHASE][2]) == H2;
        if ( !(ORH2) && H2 != 0 && static_cast<int>(PREF[IPHASE][2]) != 0) ORH2 = (H2 % static_cast<int>(PREF[IPHASE][2])) == 0;
        if ( !ORH2 ) goto L2233;

        ORH3 = static_cast<int>(PREF[IPHASE][3]) == H3;
        if ( !(ORH3) && H3 != 0 && static_cast<int>(PREF[IPHASE][3]) != 0)  ORH3 = (H3 % static_cast<int>(PREF[IPHASE][3])) == 0;
        if ( !ORH3 ) goto L2233;

        IORDR1=0;
        IORDR2=0;
        IORDR3=0;
        if (H1 != 0) IORDR1 = static_cast<int>(H1/static_cast<int>(PREF[IPHASE][1]));
        if (H2 != 0) IORDR2 = static_cast<int>(H2/static_cast<int>(PREF[IPHASE][2]));
        if (H3 != 0) IORDR3 = static_cast<int>(H3/static_cast<int>(PREF[IPHASE][3]));
        if (IORDR1 == IORDR2 && IORDR2 == IORDR3) goto L9257;
        if (IORDR1 == 0 && (IORDR2 == IORDR3)) goto L9257;
        if (IORDR2 == 0 && (IORDR3 == IORDR1)) goto L9257;
        if (IORDR3 == 0 && (IORDR1 == IORDR2)) goto L9257;
        if (IORDR1 == 0 && IORDR2 == 0) goto L9257;
        if (IORDR2 == 0 && IORDR3 == 0) goto L9257;
        if (IORDR3 == 0 && IORDR1 == 0) goto L9257;
    }
L9257:
    //     SEPARATE PHASE FOR ORIENTATION COMPLETE
    hklctl->IHKL[1][1]=H1;
    hklctl->IHKL[2][1]=H2;
    hklctl->IHKL[3][1]=H3;
    hklctl->AZ[1]=0.0;
    hklctl->IER=0;
    SMTRY2(&IPHASE);
    if(hklctl->IER != 0)goto L2233;
    LXN=2;
    if(g1->LAMDA[2] == 0.0 || g1->LAMDA[2] == g1->LAMDA[1])LXN=1;
    for (LX1=1; LX1 <= LXN; ++LX1)
    {
        SQH=SQ*g1->LAMDA[LX1]*g1->LAMDA[LX1];
        TAN2=SQH/(1.0-SQH);
        if (SQH >= 1.0) goto L3118;
        TANX=sqrt(TAN2);
        //     SHIFT DUE TO SAMPLE DISPLACEMENT AND TRANSPARENCY
        SHIFT =  DIS*sqrt(1-SQH)+TRANS*sqrt(1.-(1.-2.*SQH)*(1.-2.*SQH));
        POS=atan(TANX)/RAD+ ZERO + SHIFT;
        if ( POS > THMAXX  ||  POS < g1->THMIN ) goto L3118;
        IC=IC+1;
        XABS=1.0;
        if(g1->TMV <= 0.000001)XABS=1.0;
        PLOR=1.0/(2.0*SQH*sqrt(1.0-SQH))*XABS;
        if (cntrls->INSTRM == 2)
        {
            PLOR = PLOR * (0.95+0.05*(1.-2.*SQH)*(1.-2.*SQH));
            goto L4000;
        }
        if(cntrls->JOBTYP == 3)PLOR=PLOR*(1.+(1.-2.*SQH)*(1.-2.*SQH)*g1->CTHM);
        if(cntrls->JOBTYP == 1)PLOR=PLOR*(1.+(1.-2.*SQH)*(1.-2.*SQH)*g1->CTHM);
L4000:
        refls->IREFS[IC]=256*(256*(256*(8*IPHASE+LX1)+128+H1)+128+H2)+128+H3;
        refls->FMGNTD[IC]=MULT(H1,H2,H3,KXIS);

        //------CALCULATE FWHM FOR PSEUDOVOIGT WITH GAUSS AND LORENTZ
        if (cntrls->NPROF == _TCHZ)
        {
            refls->HALFG[IC] = (g1->U*TAN2+g1->V*TANX+g1->W+g1->ZZZ*(1+TAN2));
            if (refls->HALFG[IC] > 0.)
            {
                refls->HALFG[IC] = sqrt(refls->HALFG[IC]);
            }
            else
            {
                cout << "3" << endl;
                file6 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << "FOR PHASE NO. " << setw(IPHASE) << endl;
                file7 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << "FOR PHASE NO. " << setw(IPHASE) << endl;
                DBWSException("SQUARE OF FWHM IS NEGATIVE");
            }
            refls->HALFL[IC] = g1->ULOR*TANX+g1->VLOR/sqrt(1.0-SQH);
            refls->REFS[IC][1] = pow(( pow(refls->HALFG[IC],5.0) +2.69269*pow(refls->HALFG[IC],4.0)*refls->HALFL[IC]+2.42843*pow(refls->HALFG[IC],3.0)*pow(refls->HALFL[IC],2.0)+4.47163*pow(refls->HALFG[IC],2.0)*pow(refls->HALFL[IC],3.0)+0.07842*refls->HALFG[IC]*pow(refls->HALFL[IC],4.0)+pow(refls->HALFL[IC],5.0)),0.2);
            TLR = refls->HALFL[IC]/refls->REFS[IC][1];
            refls->GAM[IC] = 1.36603*TLR-0.47719*TLR*TLR+0.11116* pow(TLR,3.0);
        }
        else if (cntrls->NPROF == _SplitPearsonVII)
        {
            refls->REFS[IC][1] = (g1->U*TAN2+g1->V*TANX+g1->W);
        }
        else
        {
            // incorporating cotg^2  !cp may 01 97
            refls->REFS[IC][1]=(g1->U*TAN2+g1->V*TANX+g1->W+g1->ZZZ*(1+TAN2)+g1->UC/TAN2);
        }
        if (refls->REFS[IC][1] > 0.0)
        {
            refls->REFS[IC][1]= sqrt(refls->REFS[IC][1]);
        }
        else
        {
            cout << "4" << endl;
            file6 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << "FOR PHASE NO. " << setw(IPHASE) << endl;
            file7 << "  SQUARE OF FWHM NEGATIVE AT TWO-THETA" << setw(8) << setprecision(3) << POS << "FOR PHASE NO. " << setw(IPHASE) << endl;
            DBWSException("SQUARE OF FWHM IS NEGATIVE");
        }
        //L7000:
        refls->REFS[IC][2]=POS;
        refls->REFS[IC][3]=PLOR;
L3118:;
    }
    goto L2233;
L6001:
    file6 << "TOO MANY REFLECTIONS (" << setw(6) << IRS << "). INCREASE *IRS* IN THE PARAMETER STATEMENT IN THE SOURCE CODE " << endl;
    file7 << "TOO MANY REFLECTIONS (" << setw(6) << IRS << "). INCREASE *IRS* IN THE PARAMETER STATEMENT IN THE SOURCE CODE " << endl;
    cout  << "TOO MANY REFLECTIONS (" << setw(6) << IRS << "). INCREASE *IRS* IN THE PARAMETER STATEMENT IN THE SOURCE CODE " << endl;
    DBWSException("");
}

//  SUBROUTINE FINDC and SUBROUTINE COMPTON subroutine DISORDER: by Canton et all.
// Added by CPS between March-May 1997
void DBWS::FINDC(int K, int NSCAT)
{
    // ----THIS SUBROUTINE ASSIGNS AUTOMATICALLY TO ATOMS
    //     THE RIGHT CONSTANTS TO CALCULATE THE COMPTON SCATTERING

    const char PIU = '+';
    const char MENO = '-';

    int I, J, L, NA, NK, NN, NS, IOF/*, NSAVE*/;
    string NOM,NOME;

    IOF = 0;
    NS  = 0;
    //-----K = NUMBER OF PHASE UNDER CONSIDERATION
    if(K > 1)
    {
        //-----CALCULATE IOF = ALL ATOMS OF THE K-1 PHASES
        for (I = 2; I <= K; ++I) IOF = IOF + jnk->NATOM[I-1];
        //NS = NSAVE;
        NS = dc->NSAVE;
    }
    //-----DEFINE NK = NUMBER OF ATOMS OF THE K-TH PHASE
    NK = jnk->NATOM[K];
    for (I = 1; I <= NK; ++I)
    {
        for (J = 1; J <= NSCAT; ++J) if(parac->NTYP[I+IOF] == coefc->NAM[J]) goto L30;
        //-----212 = ALL THE POSSIBLE NAMES OF ATOMS AND IONS
        //L25:
        for (J = 1; J <= 212; ++J) if(parac->NTYP[I+IOF] == TBXC[J]) goto L50;
        file6 << "COMPTON SCATTERING COEFFICIENT NOT FOUND FOR " << parac->NTYP[I+IOF] << endl;
        DBWSException("COMPTON SCATTERING DATA MISSING");
L30:
        comp->PTC[I+IOF] = J;
        goto L9999;
L50:
        NOME = TBXC[J];
        //-----FIND NA = THE ATOMIC NUMBER OF I-TH ATOM
        if (J == 1 || J == 2 || J == 3)
        {
            NA = static_cast<int>(TBX[J][10]);
        }
        else
        {
            NA = static_cast<int>(TBX[J][10] + 1.0);
        }
        NS = NS + 1;
        comp->PTC[I+IOF] = NS;
        //-----PUT IN CC THE 4 COMPTON COEFFICIENTS
        for (L = 1; L <= 4; ++L) comp->CC[L][NS] = TCS[NA][L];
        coefc->NAM[NS] = NOME;

        NOM = string(NOME,0,4);
        //     FIND IN WHAT COLUMN THERE IS + OR -
        for (L=1; L <= 4; ++L)
        {
            if(NOM[L] == PIU)  goto L90;
            if(NOM[L] == MENO) goto L95;
        }
        // CASE WITH NOR + NOR -
        comp->ZEFF[NS] = NA;
        goto L9999;
        //-----CASE WITH PLUS
L90:
        L = L + 1;
        if (L > 4) DBWSException("SOMETHING IS WRONG IN ATOMIC NAME");
        NN = NOM[L];
        comp->ZEFF[NS] = NA - NN;
        goto L9999;
        //-----CASE WITH MINUS
L95:
        L = L + 1;
        if (L > 4) DBWSException("SOMETHING IS WRONG IN ATOMIC NAME");
        NN = NOM[L];
        comp->ZEFF[NS] = NA + NN;
L9999:;
    }
    //NSAVE = NS;
    dc->NSAVE = NS;
}

//  SUBROUTINE ABSORP and SUBROUTINE ARIA: by Canton et all. Added by cps between
//  march-may 1997
void DBWS::ABSORP(double MU, double SW, double TH, double* ABC)
{
    //-----THIS SUBROUTINE CORRECTS THE EXPERIMENTAL INTENSITIES FOR THE
    //                ABSORPTION EFFECTS AS REPORTED BY:
    //     1) H. P. KLUG & L. E. ALEXANDER, X-RAY DifFRACTION PROCEDURES,
    //        1970, PAG.487.
    //     2) A. IMMIRZI, ACTA CRYST. , 1980, B36, 2378-2385.
    //
    //     IT IS WRITTEN TAKING INTO ACCOUNT THE SYMMETRIC REFLECTION ARRANGEMENT
    //     ( KLUG & ALEXANDER, 1970, FIG. 5-52 PAG. 390) AND  THE  FINITE
    //     THICKNESS OR WIDTH OF THE SLAB, IN THIS SITUATION THE ABSORPTION
    //     IS GENERALLY LOW AND INCREASES SLIGHTLY WITH 2 THETA.
    //     MU = LINEAR ABSORPTION COEFFICIENT IN CM-1.
    //     SW = SAMPLE THICKNESS IN CM.

    double EX;
    EX  = ( 2.0 * MU * SW ) / sin(TH * 0.008726646);
    *ABC = 1.0 - exp(-EX);
}

// subroutine convert from dbws9411 to dbws9807
void DBWS::conv94(void)
{
    const string UPPER = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const string LOWER = " abcdefghijklmnopqrstuvwxyz";
    //	double XRYZ[10+1] = {
    //		2.748510,2.289620, 1.935970, 1.788965, 1.540520,
    //		0.709260,0.559360, 0.215947, 0.209010, 0.180195};

    //double XNOTHING;
    int I,J,K,N,IK,IOF,ISOF,IIPHAS;
    //IPLCL
    string SPG,s;


    //XNOTHING = XRYZ[1];
    if (file5.is_open()) file5.close();
    file5.open(file5name.data());			// rewind 5;
    file6 << "*** New ICF starts here with next line *** Delete everything above it" << endl;

    // line 1
    getline(file5,s);
    s = s.substr(0,70);
    file6 << setw(70) << char_->TITLE << endl;
    cout << char_->TITLE << endl;

    // line 2.1
    stringstream(s.substr(0*4,4)) >> cntrls->JOBTYP;
    stringstream(s.substr(1*4,4)) >> cntrls->NPROF;
    stringstream(s.substr(2*4,4)) >> cntrls->NPHASE;
    stringstream(s.substr(3*4,4)) >> jnk->NBCKGD;
    stringstream(s.substr(4*4,4)) >> jnk->NEXCRG;
    stringstream(s.substr(5*4,4)) >> jnk->NSCAT;
    stringstream(s.substr(6*4,4)) >> cntrls->INSTRM;
    stringstream(s.substr(7*4,4)) >> cntrls->IPREF;
    stringstream(s.substr(8*4,4)) >> cntrls->IABSR;

    cntrls->IASYM = 0;
    cntrls->NPROF = abs(cntrls->NPROF);
    cntrls->IDATA=0;
    cntrls->ISPHASE=0;
    cntrls->I2D94 =0;
    file6 << setw(4) << cntrls->JOBTYP
        << setw(4) << cntrls->NPROF
        << setw(4) << cntrls->NPHASE
        << setw(4) << jnk->NBCKGD
        << setw(4) << jnk->NEXCRG
        << setw(4) << jnk->NSCAT
        << setw(4) << cntrls->INSTRM
        << setw(4) << cntrls->IPREF
        << setw(4) << cntrls->IASYM
        << setw(4) << cntrls->IABSR
        << setw(4) << cntrls->IDATA
        << setw(4) << cntrls->ISPHASE
        << setw(4) << cntrls->I2D94 << "     LINE 2.1" << endl;

    // line 3
    getline(file5,s);
    stringstream(s.substr(0,1)) >> cntrls->IOT;
    stringstream(s.substr(1,1)) >> cntrls->IPL;
    stringstream(s.substr(2,1)) >> cntrls->IPC;
    stringstream(s.substr(3,1)) >> cntrls->MAT;
    stringstream(s.substr(4,1)) >> cntrls->NXT;
    stringstream(s.substr(5,1)) >> cntrls->LST1;
    stringstream(s.substr(6,1)) >> cntrls->LST2;
    stringstream(s.substr(7,1)) >> cntrls->LST3;
    stringstream(s.substr(8,1)) >> cntrls->IPL1;
    stringstream(s.substr(9,1)) >> cntrls->IPL2;
    stringstream(s.substr(10,1)) >> cntrls->IPLST;
    cntrls->IPLOSS = 0;
    //	IPLCL  = 0;
    cntrls->IPLCAL = 0;
    cntrls->IPLPOL = 0;
    cntrls->IPLCOM = 0;
    cntrls->IPLDIS = 0;
    cntrls->IPLAM  = 0;
    cntrls->IPBIG  = 0;
    file6 << setw(1) << cntrls->IOT
        << setw(1) << cntrls->IPL
        << setw(1) << cntrls->IPC
        << setw(1) << cntrls->MAT
        << setw(1) << cntrls->NXT << " "
        << setw(1) << cntrls->LST1
        << setw(1) << cntrls->LST2
        << setw(1) << cntrls->LST3
        << setw(1) << cntrls->IPL1
        << setw(1) << cntrls->IPL2 << " "
        << setw(1) << cntrls->IPLST
        << setw(1) << cntrls->IPLOSS
        << setw(1) << cntrls->IPLCAL
        << setw(1) << cntrls->IPLPOL
        << setw(1) << cntrls->IPLCOM << " "
        << setw(1) << cntrls->IPLDIS
        << setw(1) << cntrls->IPLAM
        << setw(1) << cntrls->IPBIG << "                                    LINE 3" << endl;

    getline(file5,s);
    stringstream(s.substr(0*8,8)) >> g1->LAMDA[1];
    stringstream(s.substr(1*8,8)) >> g1->LAMDA[2];
    stringstream(s.substr(2*8,8)) >> params->RATIO[2];
    stringstream(s.substr(3*8,8)) >> g1->BKPOS;
    stringstream(s.substr(4*8,8)) >> g1->WDT;
    stringstream(s.substr(5*8,8)) >> g1->CTHM;
    stringstream(s.substr(6*8,8)) >> g1->TMV;
    stringstream(s.substr(7*8,8)) >> g1->RLIM;
    cntrls->SW = 0.0;
    file6 << setw(8) << setprecision(5) << g1->LAMDA[1]
    << setw(8) << setprecision(5) << g1->LAMDA[2]
    << setw(8) << setprecision(5) << params->RATIO[2]
    << setw(8) << setprecision(4) << g1->BKPOS
        << setw(8) << setprecision(4) << g1->WDT
        << setw(8) << setprecision(4) << g1->CTHM
        << setw(8) << setprecision(4) << g1->TMV
        << setw(8) << setprecision(4) << g1->RLIM
        << setw(8) << setprecision(4) << cntrls->SW << endl;

    //  line 5
    getline(file5,s);
    stringstream(s.substr(0,4)) >> cntrls->MCYCLE;
    stringstream(s.substr(4,4)) >> cntrls->EPS;
    stringstream(s.substr(8,4)) >> params->RELAX[1];
    stringstream(s.substr(12,4)) >> params->RELAX[2];
    stringstream(s.substr(16,4)) >> params->RELAX[3];
    stringstream(s.substr(20,4)) >> params->RELAX[4];
    stringstream(s.substr(24,8)) >> g1->THMIN;
    stringstream(s.substr(32,8)) >> g1->STEP;
    stringstream(s.substr(40,8)) >> g1->THMAX;
    file6 << setw(4) << cntrls->MCYCLE
        << setw(4) << setprecision(2) << cntrls->EPS
        << setw(4) << setprecision(2) << params->RELAX[1]
    << setw(4) << setprecision(2) << params->RELAX[2]
    << setw(4) << setprecision(2) << params->RELAX[3]
    << setw(4) << setprecision(2) << params->RELAX[4]
    << setw(8) << setprecision(3) << g1->THMIN
        << setw(8) << setprecision(3) << g1->STEP
        << setw(8) << setprecision(3) << g1->THMAX << "         CYCLS EPS RELAX P_CALC" << endl;
    if(jnk->NBCKGD < 2)goto L120;

    // line 6(*)
    for (I=1; I <= jnk->NBCKGD; ++I)
    {
        getline(file5,s);
        stringstream(s.substr(0,8)) >> jnk->POS[I];
        stringstream(s.substr(8,8)) >> jnk->BCK[I];
    }
    for (I=1; I <= jnk->NBCKGD; ++I)
    {
        file6 << setw(8) << setprecision(2) << jnk->POS[I]
        << setw(8) << setprecision(2) << jnk->BCK[I] << endl;
    }

    // line 7(*)
L120:
    if(jnk->NEXCRG <= 0)goto L122;
    for (I=1; I <= jnk->NEXCRG; ++I)
    {
        getline(file5,s);
        stringstream(s.substr(0,8)) >> jnk->ALOW[I];
        stringstream(s.substr(8,8)) >> jnk->AHIGH[I];
    }
    for (I=1; I <= jnk->NEXCRG; ++I)
    {
        file6 << setw(8) << setprecision(2) << jnk->ALOW[I]
        << setw(8) << setprecision(2) << jnk->AHIGH[I]
        << "                                         EXCLUDED REGION" << endl;
    }

    //  line 8(*) read
L122:
    if(jnk->NSCAT <= 0)goto L124;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        if(cntrls->JOBTYP == 1 || cntrls->JOBTYP == 3)
        {
            getline(file5,s);
            coefc->NAM[I] = s.substr(0,4);
            stringstream(s.substr(4,8)) >> coeff->DFP[I];
            stringstream(s.substr(12,8)) >> coeff->XMAS[I];
            goto L125;
        }
        getline(file5,s);
        coefc->NAM[I] = s.substr(0,4);
        stringstream(s.substr(4,8)) >> coeff->DFP[I];
        stringstream(s.substr(12,8)) >> coeff->DFPP[I];
        stringstream(s.substr(20,8)) >> coeff->XMAS[I];
        K=0;
L126:
        getline(file5,s);
        for (J=1; J <= 9; ++J) stringstream(s.substr((J-1)*8,8)) >> coeff->AC[J][I];
        if(coeff->AC[1][I] == -100.0) coeff->COEF(&I,&K);
        if(coeff->AC[3][I] != 0.0) goto L125;
        K=K+1;
        coeff->POSI[K]=coeff->AC[1][I];
        coeff->SCAT[K]=coeff->AC[2][I];
        if(K <= 29) goto L126;
        file6 << "TOO MANY SCATTERING TABLE ENTRIES" << endl;
        DBWSException("7700");
L125:;
    }
    goto L124;
L124:

    //  line 8 (write)
    if(jnk->NSCAT <= 0)goto L813;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        if (cntrls->JOBTYP  ==  1 || cntrls->JOBTYP == 3) goto L809;
        // line 8.1 XRD (*)
        file6 << setw(4) << coefc->NAM[I]
        << setw(8) << setprecision(4) << coeff->DFP[I]
        << setw(8) << setprecision(4) << coeff->DFPP[I]
        << setw(8) << setprecision(4) << coeff->XMAS[I]
        << "                             SCATTERING SET" << setw(2) << jnk->NSCAT << endl;
        goto L814;

L809:
        // line 8.1 ND(*)
        file6 << setw(4) << coefc->NAM[I]
        << setw(8) << setprecision(4) << coeff->DFP[I]
        << setw(8) << setprecision(4) << coeff->XMAS[I]
        << "                                     " << setw(2) << jnk->NSCAT << endl;


L814:
        // line 8.2 XRD(*)
        if(cntrls->JOBTYP == 0 || cntrls->JOBTYP == 2)
        {
            for (J=1; J <= 9; ++J) file6 << setw(8) << setprecision(5) << coeff->AC[J][I];
            file6 << endl;
        }
    }
L813:

    // line 9
    getline(file5,s);
    stringstream(s.substr(0,8)) >> cntrls->MAXS;
    file6 << setw(8) << cntrls->MAXS << "                                                  PARAMS REFINED" << endl;

    //  line 10 (read)
    getline(file5,s);
    stringstream(s.substr(0*8,8)) >> params->GLB[1];
    stringstream(s.substr(1*8,8)) >> params->GLB[10];
    stringstream(s.substr(2*8,8)) >> params->GLB[11];
    stringstream(s.substr(3*8,8)) >> params->GLB[8];
    stringstream(s.substr(4*8,8)) >> params->GLB[9];
    stringstream(s.substr(5*8,8)) >> params->GLB[12];
    stringstream(s.substr(6*8,8)) >> params->GLB[13];

    getline(file5,s);
    stringstream(s.substr(0*8,8)) >> params->AGLB[1];
    stringstream(s.substr(1*8,8)) >> params->AGLB[10];
    stringstream(s.substr(2*8,8)) >> params->AGLB[11];
    stringstream(s.substr(3*8,8)) >> params->AGLB[8];
    stringstream(s.substr(4*8,8)) >> params->AGLB[9];
    stringstream(s.substr(5*8,8)) >> params->AGLB[12];
    stringstream(s.substr(6*8,8)) >> params->AGLB[13];

    // line 10.1 (write)
    file6 << setw(8) << setprecision(4) << params->GLB[1]
    << setw(8) << setprecision(4) << params->GLB[10]
    << setw(8) << setprecision(4) << params->GLB[11]
    << setw(8) << setprecision(4) << params->GLB[8]
    << setw(8) << setprecision(4) << params->GLB[9]
    << setw(8) << setprecision(4) << params->GLB[12]
    << setw(8) << setprecision(4) << params->GLB[13]
    << " ZER DISP TRANS p q r t" << endl;
    // line 10.11 (write)
    file6 << setw(8) << setprecision(4) << params->AGLB[1]
    << setw(8) << setprecision(4) << params->AGLB[10]
    << setw(8) << setprecision(4) << params->AGLB[11]
    << setw(8) << setprecision(4) << params->AGLB[8]
    << setw(8) << setprecision(4) << params->AGLB[9]
    << setw(8) << setprecision(4) << params->AGLB[12]
    << setw(8) << setprecision(4) << params->AGLB[13]
    << " CODEWORDS" << endl;

    // line 10.3 (read & write)
    if(jnk->NBCKGD != 0)goto L49;
    getline(file5,s);
    stringstream(s.substr(0*9,9)) >> params->GLB[2];
    stringstream(s.substr(1*9,9)) >> params->GLB[3];
    stringstream(s.substr(2*9,9)) >> params->GLB[4];

    getline(file5,s);
    stringstream(s.substr(0*9,9)) >> params->AGLB[2];
    stringstream(s.substr(1*9,9)) >> params->AGLB[3];
    stringstream(s.substr(2*9,9)) >> params->AGLB[4];

    file6 << setw(9) << setprecision(2) << params->GLB[2]
    << setw(9) << setprecision(2) << params->GLB[3]
    << setw(9) << setprecision(2) << params->GLB[4]
    << setw(9) << setprecision(2) << params->GLB[5]
    << setw(9) << setprecision(2) << params->GLB[6]
    << setw(9) << setprecision(2) << params->GLB[7] << "   BACKGROUND" << endl
        << setw(9) << setprecision(4) << params->AGLB[2]
    << setw(9) << setprecision(4) << params->AGLB[3]
    << setw(9) << setprecision(4) << params->AGLB[4]
    << setw(9) << setprecision(4) << params->AGLB[5]
    << setw(9) << setprecision(4) << params->AGLB[6]
    << setw(9) << setprecision(4) << params->AGLB[7] << "   CODEWORDS" << endl;
L49:
    // read AND rewrite for phases
    for (K=1; K <= cntrls->NPHASE; ++K)
    {
        // line 11.1
        getline(file5,s);
        char_->PHSNM[K] = s.substr(0,50);
        file6 << setw(50) << char_->PHSNM[K] << "       PHASE NUMBER " << setw(2) << K << endl;

        // line 11.2
        getline(file5,s);
        stringstream(s.substr(0,4)) >> jnk->NATOM[K];
        stringstream(s.substr(4,4)) >> jnk->NMOL[K];
        stringstream(s.substr(16,4)) >> jnk->PREF[K][1];
        stringstream(s.substr(20,4)) >> jnk->PREF[K][2];
        stringstream(s.substr(24,4)) >> jnk->PREF[K][3];
        multip->SAQF[K] = 1.0;
        file6 << setw(4) << jnk->NATOM[K]
        << setw(4) << jnk->NMOL[K]
        << setw(7) << setprecision(4) << multip->SAQF[K] << " "
            << setw(4) << setprecision(1) << jnk->PREF[K][1]
        << setw(4) << setprecision(1) << jnk->PREF[K][2]
        << setw(4) << setprecision(1) << jnk->PREF[K][3]
        << setw(7) << setprecision(2) << multip->WTIS[K]
        << "                      #ATMS #FU AFQPA PREFDIR ISWT" << endl;
        N=jnk->NATOM[K];

        // line 11.3
        getline(file5,s);
        char_->SYMB[K] = s.substr(0,20);
        file6 << setw(20) << char_->SYMB[K] << "                                     SPACE GROUP" << endl;
        SPG = " " + char_->SYMB[K];
        for (I=1; I <= 20; ++I)
        {
            // convert lower case to upper case
            for (IK=1; IK <= 26; ++IK) if (SPG[I] == LOWER[IK]) SPG[I]=UPPER[IK];
            // finish conversion
        }
        if (convert->ICNVT != 1) file6 << "THE SPACE GROUP IS " << SPG << endl;
        spgcom->SPGP(SPG);
        // getting multiplicity of each phase !cp jun 96)
        simoper->ISIMOP=1;
        RTMT(&cntrls->IPL1,&K);
        multip->XMLTP[K]=multip->MLTPHASE;
        cout << "General position multiplicity is " << setw(4) << multip->MLTPHASE << " for phase " << setw(2) << K << endl;

        // line 11.4
        //-----READ  FOR EACH ATOM
        IOF=0;
        if (K > 1)
        {
            for (IIPHAS=2; IIPHAS <= K; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }

        // line 11.41 and 11.42
        for (I=1; I <= N; ++I)
        {
            getline(file5,s);
            parac->ATEXT[I+IOF] = s.substr(0,4);
            parac->NTYP[I+IOF] = s.substr(4,4);
            stringstream(s.substr(16,8)) >> params->XL[I+IOF][1];
            stringstream(s.substr(16,8)) >> params->XL[I+IOF][2];
            stringstream(s.substr(24,8)) >> params->XL[I+IOF][3];
            stringstream(s.substr(32,8)) >> params->XL[I+IOF][4];
            stringstream(s.substr(40,8)) >> params->XL[I+IOF][5];

            getline(file5,s);
            stringstream(s.substr(16,8)) >> params->A[I+IOF][1];
            stringstream(s.substr(24,8)) >> params->A[I+IOF][2];
            stringstream(s.substr(32,8)) >> params->A[I+IOF][3];
            stringstream(s.substr(40,8)) >> params->A[I+IOF][4];
            stringstream(s.substr(48,8)) >> params->A[I+IOF][5];

            getline(file5,s);
            stringstream(s.substr(0,8)) >> params->XL[I+IOF][6];
            stringstream(s.substr(8,8)) >> params->XL[I+IOF][7];
            stringstream(s.substr(16,8)) >> params->XL[I+IOF][8];
            stringstream(s.substr(24,8)) >> params->XL[I+IOF][9];
            stringstream(s.substr(32,8)) >> params->XL[I+IOF][10];
            stringstream(s.substr(40,8)) >> params->XL[I+IOF][11];

            getline(file5,s);
            stringstream(s.substr(0,8)) >> params->A[I+IOF][6];
            stringstream(s.substr(8,8)) >> params->A[I+IOF][7];
            stringstream(s.substr(16,8)) >> params->A[I+IOF][8];
            stringstream(s.substr(24,8)) >> params->A[I+IOF][9];
            stringstream(s.substr(32,8)) >> params->A[I+IOF][10];
            stringstream(s.substr(40,8)) >> params->A[I+IOF][11];
        }
        // Creating MURT and 'So'
        for (ISOF=1; ISOF <= N; ++ISOF) params->XL[ISOF+IOF][5]=1.0;
        for (I=1; I <= N; ++I)
        {
            file6 << setw(4) << parac->ATEXT[I+IOF] << "    # "
                << setw(4) << parac->NTYP[I+IOF] << "  "
                << setw(8) << setprecision(5) << params->XL[I+IOF][1]
            << setw(8) << setprecision(5) << params->XL[I+IOF][2]
            << setw(8) << setprecision(5) << params->XL[I+IOF][3]
            << setw(8) << setprecision(5) << params->XL[I+IOF][4]
            << setw(8) << setprecision(5) << params->XL[I+IOF][5]
            << "  LBL M NTYP x y z B So" << endl
                << "                "
                << setw(8) << setprecision(2) << params->A[I+IOF][1]
            << setw(8) << setprecision(2) << params->A[I+IOF][2]
            << setw(8) << setprecision(2) << params->A[I+IOF][3]
            << setw(8) << setprecision(2) << params->A[I+IOF][4]
            << setw(8) << setprecision(2) << params->A[I+IOF][5]
            << "  CODEWORDS" << endl
                << setw(8) << setprecision(5) << params->XL[I+IOF][6]
            << setw(8) << setprecision(5) << params->XL[I+IOF][7]
            << setw(8) << setprecision(5) << params->XL[I+IOF][8]
            << setw(8) << setprecision(5) << params->XL[I+IOF][9]
            << setw(8) << setprecision(5) << params->XL[I+IOF][10]
            << setw(8) << setprecision(5) << params->XL[I+IOF][11]
            << "          BETAS" << endl
                << setw(8) << setprecision(2) << params->A[I+IOF][6]
            << setw(8) << setprecision(2) << params->A[I+IOF][7]
            << setw(8) << setprecision(2) << params->A[I+IOF][8]
            << setw(8) << setprecision(2) << params->A[I+IOF][9]
            << setw(8) << setprecision(2) << params->A[I+IOF][10]
            << setw(8) << setprecision(2) << params->A[I+IOF][11]
            << "          CODEWORDS" << endl;

        }
        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->PAR[K][1];
        stringstream(s.substr(8,8)) >> params->PAR[K][2];
        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][1];
        stringstream(s.substr(8,8)) >> params->APAR[K][2];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][3];
        stringstream(s.substr(1*8,8)) >> params->PAR[K][4];
        stringstream(s.substr(2*8,8)) >> params->PAR[K][5];
        stringstream(s.substr(3*8,8)) >> params->PAR[K][20];
        stringstream(s.substr(4*8,8)) >> params->PAR[K][15];
        stringstream(s.substr(5*8,8)) >> params->PAR[K][16];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][3];
        stringstream(s.substr(1*8,8)) >> params->APAR[K][4];
        stringstream(s.substr(2*8,8)) >> params->APAR[K][5];
        stringstream(s.substr(3*8,8)) >> params->APAR[K][20];
        stringstream(s.substr(4*8,8)) >> params->APAR[K][15];
        stringstream(s.substr(5*8,8)) >> params->APAR[K][16];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][6];
        stringstream(s.substr(1*8,8)) >> params->PAR[K][7];
        stringstream(s.substr(2*8,8)) >> params->PAR[K][8];
        stringstream(s.substr(3*8,8)) >> params->PAR[K][9];
        stringstream(s.substr(4*8,8)) >> params->PAR[K][10];
        stringstream(s.substr(5*8,8)) >> params->PAR[K][11];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][6];
        stringstream(s.substr(1*8,8)) >> params->APAR[K][7];
        stringstream(s.substr(2*8,8)) >> params->APAR[K][8];
        stringstream(s.substr(3*8,8)) >> params->APAR[K][9];
        stringstream(s.substr(4*8,8)) >> params->APAR[K][10];
        stringstream(s.substr(5*8,8)) >> params->APAR[K][11];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][12];
        stringstream(s.substr(1*8,8)) >> params->PAR[K][13];
        stringstream(s.substr(2*8,8)) >> params->PAR[K][14];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][12];
        stringstream(s.substr(1*8,8)) >> params->APAR[K][13];
        stringstream(s.substr(2*8,8)) >> params->APAR[K][14];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][17];
        stringstream(s.substr(1*8,8)) >> params->PAR[K][18];
        stringstream(s.substr(2*8,8)) >> params->PAR[K][19];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][17];
        stringstream(s.substr(1*8,8)) >> params->APAR[K][18];
        stringstream(s.substr(2*8,8)) >> params->APAR[K][19];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][24];
        stringstream(s.substr(1*8,8)) >> params->PAR[K][25];
        stringstream(s.substr(2*8,8)) >> params->PAR[K][26];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][24];
        stringstream(s.substr(1*8,8)) >> params->APAR[K][25];
        stringstream(s.substr(2*8,8)) >> params->APAR[K][26];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->PAR[K][27];
        getline(file5,s);
        stringstream(s.substr(0*8,8)) >> params->APAR[K][27];
        params->PAR[K][21]  = 0.0;
        params->APAR[K][21] = 0.0;
        file6 << scientific << setw(8) << setprecision(3) << params->PAR[K][1]
        << fixed << setw(8) << setprecision(4) << params->PAR[K][2]
        << "                                         SCALE Bo(OVERALL)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][1]
        << setw(8) << setprecision(2) << params->APAR[K][2] << endl
            << setw(8) << setprecision(5) << params->PAR[K][3]
        << setw(8) << setprecision(5) << params->PAR[K][4]
        << setw(8) << setprecision(5) << params->PAR[K][5]
        << setw(8) << setprecision(5) << params->PAR[K][21]
        << setw(8) << setprecision(5) << params->PAR[K][20]
        << setw(8) << setprecision(5) << params->PAR[K][15]
        << setw(8) << setprecision(5) << params->PAR[K][16]
        << " U V W CT Z X Y" << endl
            << setw(8) << setprecision(2) << params->APAR[K][3]
        << setw(8) << setprecision(2) << params->APAR[K][4]
        << setw(8) << setprecision(2) << params->APAR[K][5]
        << setw(8) << setprecision(2) << params->APAR[K][21]
        << setw(8) << setprecision(2) << params->APAR[K][20]
        << setw(8) << setprecision(2) << params->APAR[K][15]
        << setw(8) << setprecision(2) << params->APAR[K][16] << endl
            << setw(8) << setprecision(4) << params->PAR[K][6]
        << setw(8) << setprecision(4) << params->PAR[K][7]
        << setw(8) << setprecision(4) << params->PAR[K][8]
        << setw(8) << setprecision(4) << params->PAR[K][9]
        << setw(8) << setprecision(4) << params->PAR[K][10]
        << setw(8) << setprecision(4) << params->PAR[K][11]
        << "         CELL PARAMETERS" << endl
            << setw(8) << setprecision(2) << params->APAR[K][6]
        << setw(8) << setprecision(2) << params->APAR[K][7]
        << setw(8) << setprecision(2) << params->APAR[K][8]
        << setw(8) << setprecision(2) << params->APAR[K][9]
        << setw(8) << setprecision(2) << params->APAR[K][10]
        << setw(8) << setprecision(2) << params->APAR[K][11] << endl
            << setw(8) << setprecision(5) << params->PAR[K][12]
        << setw(8) << setprecision(5) << params->PAR[K][13]
        << setw(8) << setprecision(5) << params->PAR[K][14]
        << "                                 PREF1 PREF2 R/RCF_ASYM" << endl
            << setw(8) << setprecision(2) << params->APAR[K][12]
        << setw(8) << setprecision(2) << params->APAR[K][13]
        << setw(8) << setprecision(2) << params->APAR[K][14] << endl
            << setw(8) << setprecision(5) << params->PAR[K][17]
        << setw(8) << setprecision(5) << params->PAR[K][18]
        << setw(8) << setprecision(5) << params->PAR[K][19]
        << "                                 NA NB NC (MIX_PARAMS)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][17]
        << setw(8) << setprecision(2) << params->APAR[K][18]
        << setw(8) << setprecision(2) << params->APAR[K][19] << endl
            << setw(8) << setprecision(5) << params->PAR[K][24]
        << setw(8) << setprecision(5) << params->PAR[K][25]
        << setw(8) << setprecision(5) << params->PAR[K][26]
        << "                                 NA NB NC (HIGH SIDE)" << endl
            << setw(8) << setprecision(2) << params->APAR[K][24]
        << setw(8) << setprecision(2) << params->APAR[K][25]
        << setw(8) << setprecision(2) << params->APAR[K][26] << endl
        << setw(8) << setprecision(4) << params->PAR[K][27]
        << "                                                 PEARSON ASYM.FACTOR" << endl
            << setw(8) << setprecision(2) << params->APAR[K][27] << endl;
    }
    return;
    //L99999:
    //	DBWSException("END OF FILE TAPE5");
}

// SUBROUTINE GSASREAD * READ GSAS FORMATTED DATA FILE
void DBWS::GSASREAD(void)
{
    int I, J, K, L4, L5, L10, L11, L12, L13;
    int LAB[20+1];
    int NCTR[IDSZ+1];
    string s,TEST,DATAID;

    getline(file4,s);
    DATAID = s.substr(0,72);
    J=0;
    K=1;
L10:
    getline(file4,s);
    TEST = " " + s.substr(0,72);
    if( TEST.substr(1,4) != "BANK") goto L10;
L31:
    for (I=K; I <= 72; ++I)
    {
        if( TEST[I] == ' ')
        {
            J=J+1;
            LAB[J]=I-1;
            goto L21;
        }
    }

L21:
    for (K=I; K <= 66; ++K)
    {
        if(TEST[K] != ' ')
        {
            J=J+1;
            LAB[J]=K;
            goto L31;
        }
    }
    L4=LAB[4];
    L5=LAB[5];
    L10=LAB[10];
    L11=LAB[11];
    L12=LAB[12];
    L13=LAB[13];
    stringstream(TEST.substr(L4,L5-L4+1)) >> datax->NPTS;			// npts
    stringstream(TEST.substr(L10,L11-L10+1)) >> g1->THMIN;			// start*100
    stringstream(TEST.substr(L12,L13-L12+1)) >> g1->STEP;			// step*100
    g1->THMIN=g1->THMIN/100;
    g1->STEP=g1->STEP/100;
    g1->THMAX =g1->THMIN+datax->NPTS*g1->STEP;
    cout << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    file6 << "    DATA ID " << setw(56) << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;

    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    for (I=1; I <= datax->NPTS; ++I)
    {
        file4 >> NCTR[I] >> datax->Y[I];			//format(10(i2,f6.0))
    }
    for (I=1; I <= datax->NPTS; ++I)
    {
        if (NCTR[I] == 0) NCTR[I]=1;
        if (datax->Y[I] == 0.0) datax->Y[I]=0.0;		// TODO: ???????
    }
    return;
    //L99998:
    //	DBWSException("END OF FILE unit=4");
}

// SUBROUTINE TO READ PHILIPS UDF DATA FILE
void DBWS::PHILIPSREAD(void)
{
    int I,LB1,LB2,LB3;
    string s,TEST,DATAID,ATHMIN,ATHMAX;

    getline(file4,s);
    DATAID = s.substr(0,56);
L10:
    getline(file4,s);
    TEST = " " + s.substr(0,56);
    for (I=1; I <= 56; ++I)
    {
        if(TEST[I] == ',')
        {
            LB1=I;
            goto L31;
        }
    }
L31:
    if( TEST.substr(1,LB1-1) != "DataAngleRange") goto L10;
    //                            DataAngleRange
    for (I=LB1+1; I <= 56; ++I)
    {
        if (TEST[I] == ',')
        {
            LB2=I;
            goto L41;
        }
    }

L41:
    for (I=LB2+1; I <= 56; ++I)
    {
        if(TEST[I] == ',')
        {
            LB3=I;
            goto L51;
        }
    }

L51:
    ATHMIN=TEST.substr(LB1+1,LB2-1 - (LB1+1)+1);
    ATHMAX=TEST.substr(LB2+1,LB3-1 - (LB2+1)+1);
    getline(file4,s);
    TEST = s.substr(0,13);
    stringstream(s.substr(13,8)) >> g1->STEP;
L30:
    getline(file4,s);
    TEST = " " + s.substr(0,56);
    for (I=1; I <= 56; ++I)
    {
        if( TEST[I] == ',') LB1=I;
    }

    if( TEST.substr(1,LB1-1-1+1) != "RawScan") goto L30;
    //                            RawScan
    stringstream(ATHMIN) >> g1->THMIN;
    stringstream(ATHMAX) >> g1->THMAX;

    cout << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    file6 << "    DATA ID " << setw(56) << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    datax->NPTS=static_cast<int>((g1->THMAX-g1->THMIN)/g1->STEP+1.5);
    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    for (I=1; I <= datax->NPTS; ++I) file4 >> datax->Y[I];
    return;
    //L99998:
    //	DBWSException("END OF FILE unit=4");
}

// suboutine (QPAINIT) to compute mass fractions before starting the refinement
void DBWS::qpainit(void)
{
    int I,N,IP,IOF,ICOCO,IIPHAS,IINNMOL;
    double V0,FT, XFAC, SFIS, ARGCOS, WTOTAL;
    //VOSQ
    //ARG1, ARG2, ARG3,
    double VOL[99+1];
    double W[99+1];
    double XMASS[99+1];
    double FR[99+1];
    double FRP[99+1];

    file6 << "       >>> QPA before starting the refinement <<<" << endl
        << "       >>                                    <<<" << endl;
    for (IP=1; IP <= cntrls->NPHASE; ++IP)
    {
        DIRECT(dircv->DCSM,dircv->DCV,&IP);
        multip->TMASSA[IP]=0.0;
        IOF=0;
        if(IP > 1)
        {
            for (IIPHAS=2; IIPHAS <= IP; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }
        N=jnk->NATOM[IP];
        for (I=1; I <= N; ++I)
        {
            ICOCO=params->PTR[I+IOF];
            multip->TMASSA[IP] = multip->TMASSA[IP] + params->XL[I+IOF][5]*coeff->XMAS[ICOCO]*multip->XMLTP[IP];
        }
        XFAC = 3.141592654 / 180.000000;
        dircv->DCV[4] = XFAC * dircv->DCV[4];
        dircv->DCSM[4][4] = dircv->DCSM[4][4] * XFAC;
        dircv->DCV[5] = XFAC * dircv->DCV[5];
        dircv->DCSM[5][5] = dircv->DCSM[5][5] * XFAC;
        dircv->DCV[6] = dircv->DCV[6] * XFAC;
        dircv->DCSM[6][6] = dircv->DCSM[6][6] * XFAC;

        //-----Calculations of VOLUME and SVZM (=W) for each phase
        ARGCOS= 1-pow((cos(dircv->DCV[4])),2)-pow((cos(dircv->DCV[5])),2)-pow((cos(dircv->DCV[6])),2) + 2 * (cos(dircv->DCV[4])) * (cos(dircv->DCV[5])) * (cos(dircv->DCV[6]));
        V0 = dircv->DCV[1] * dircv->DCV[2] * dircv->DCV[3];
        VOL[IP] = V0 * sqrt(ARGCOS);
        //		VOSQ = 0.5*VOL[IP]/ARGCOS;
        //		ARG1 = VOSQ*(2 * cos(dircv->DCV[4]) * sin(dircv->DCV[4]) - 2*sin(dircv->DCV[4]) *cos(dircv->DCV[5]) *cos(dircv->DCV[6])) * dircv->DCSM[4][4];
        //		ARG2 = VOSQ*(2 * cos(dircv->DCV[5]) * sin(dircv->DCV[5]) - 2*sin(dircv->DCV[5]) *cos(dircv->DCV[4]) *cos(dircv->DCV[6])) * dircv->DCSM[5][5];
        //		ARG3 = VOSQ*(2 * cos(dircv->DCV[6]) * sin(dircv->DCV[6]) - 2*sin(dircv->DCV[6]) *cos(dircv->DCV[4]) *cos(dircv->DCV[5])) * dircv->DCSM[6][6];
        W[IP] = params->PAR[IP][1] * multip->TMASSA[IP] * VOL[IP]/multip->SAQF[IP];
        file6 << "Volume("
            << setw(2) << IP << ")= "
            << setw(9) << setprecision(3) << VOL[IP]
        << " UCW= "
            << setw(7) << setprecision(2) << multip->TMASSA[IP]
        << " U.C.Density = "
            << setw(7) << setprecision(3) << 1.66113*multip->TMASSA[IP]/VOL[IP]
        << " gr/cm^3" << endl;
    }
    // ****** QUANTITATIVE ANALYSIS ***************
    file6 << endl;
    WTOTAL = 0.000000;
    for (I = 1; I <= cntrls->NPHASE; ++I) WTOTAL = WTOTAL + W[I];
    for (I = 1; I <= cntrls->NPHASE; ++I) XMASS[I] = 100.0 * W[I] / WTOTAL;
    IINNMOL = 0;
    for (I = 1; I <= cntrls->NPHASE; ++I)
    {
        if (IINNMOL == 1) goto L2713;
        if (jnk->NMOL[I] == 0) IINNMOL=1;
L2713:;
    }
    if (IINNMOL == 1)
    {
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            // ** printing results
            file6 << "PHASE = "
                << setw(2) << I
                << " => %MASS = "
                << setw(6) << setprecision(2) << XMASS[I]
            << "%MOLAR = NOT COMPUTED" << endl;
        }
    }
    else
    {
        // ****    CALCULATION OF MOLAR FRACTION  ****
        FT = 0.0000000;
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            FRP[I] = XMASS[I] * jnk->NMOL[I] / multip->TMASSA[I];
            FT = FT + FRP[I];
        }
        for (I = 1; I <= cntrls->NPHASE; ++I)
        {
            FR[I] = 100.0 * FRP[I] / FT;
            // ** printing results
            file6 << "PHASE = "
                << setw(2) << I
                << " => %MASS = "
                << setw(6) << setprecision(2) << XMASS[I]
            << "  %MOLAR = "
                << setw(6) << setprecision(2) << FR[I] << endl;
        }
    }
    if(cntrls->ISPHASE != 0)
    {
        file6 << endl << "Considering Amorphous Content:" << endl;
        SFIS=multip->WTIS[cntrls->ISPHASE]/XMASS[cntrls->ISPHASE];
        if(SFIS > 1.0)
        {
            file6 << "PROBLEM:Amount of Internal Standard (Phase #"
                << setw(2) << cntrls->ISPHASE
                << ") is less than the specified "
                << setw(6) << setprecision(2) << multip->WTIS[cntrls->ISPHASE]
            << "%." << endl
                << "Amorphous content not computed. Check ISWT in line 11.2 for this phase" << endl;
            goto L2720;
        }
        for (I=1; I <= cntrls->NPHASE; ++I)
        {
            file6 << "PHASE = " << setw(2) << I << " => %MASS = " << setw(6) << setprecision(2) << XMASS[I]*SFIS << endl;
        }
        file6 << "AMORPHOUS  => %MASS = " << setw(6) << setprecision(2) <<100*(1.0-SFIS)  << endl;
    }
L2720:
    file6 << endl;
    return;
}

// SUBROUTINE TO READ RIGAKU DATA FILE
void  DBWS::rigakuread(void)
{
    int I;
    string s,TEST,ASTEP,ANPTS,ATHMIN,ATHMAX;

L10:
    getline(file4,s);
    TEST = s.substr(0,72);
    if(TEST.substr(1,6) == "*START")
    {
        for (I=1; I <= 56; ++I)
        {
            if(TEST[I] == '=')
            {
                ATHMIN=TEST.substr(I+1,I+11-(I+1)+1);
                goto L10;
            }
        }
    }
    if(TEST.substr(1,5) == "*STOP")
    {
        for (I=1; I <= 56; ++I)
        {
            if(TEST[I] == '=')
            {
                ATHMAX=TEST.substr(I+1,I+11-(I+1)+1);
                goto L10;
            }
        }
    }
    if(TEST.substr(1,5) == "*STEP")
    {
        for (I=1; I <= 56; ++I)
        {
            if(TEST[I] == '=')
            {
                ASTEP=TEST.substr(I+1,I+8-(I+1)+1);
                goto L10;
            }
        }
    }
    if(TEST.substr(1,6) == "*COUNT" && TEST.substr(1,7) != "*COUNTE")
    {
        for (I=1; I <= 56; ++I)
        {
            if(TEST[I] == '=')
            {
                ANPTS=TEST.substr(I+1,I+8-(I+1)+1);
                goto L20;
            }
        }
    }
    goto L10;
L20:
    stringstream(ATHMIN) >> g1->THMIN;
    stringstream(ASTEP) >> g1->STEP;
    stringstream(ATHMAX) >> g1->THMAX;
    stringstream(ANPTS) >> datax->NPTS;
    cout << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    //file6 << "    DATA ID " << setw(56) << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    // read the data
    for (I=1; I <= datax->NPTS; ++I) file4 >> datax->Y[I];
    return;
    //L99998:
    //	DBWSException("END OF FILE unit=4");
    //L99999:
    //	DBWSException("IS THE FILE NOT RIGAKU FORMATED?");
}

void DBWS::readasc(void)
{
    int I;

    for (I=1; I <= 56; ++I)
    {
        if( labels->TEST[I] == ':')
        {
            labels->LB1=I;
            goto L31;
        }
    }
L31:
    for (I=labels->LB1+1; I <= 56; ++I)
    {
        if( labels->TEST[I] != ' ')
        {
            labels->LB2=I;
            goto L41;
        }
    }
L41:
    for (I=labels->LB2; I <= 56; ++I)
    {
        if( labels->TEST[I] == ' ')
        {
            labels->LB3=I-1;
            goto L51;
        }
    }
L51:;
}

// subroutine to read SCINTAG TXT file
void DBWS::scintag(void)
{
    int I;
    double X1,X2,X3;
    string s,ASTEP, DATAID, ATHMIN, ATHMAX;

    getline(file4,s);
    DATAID = " "+s.substr(0,56);
L10:
    getline(file4,s);
    labels->TEST = " "+s.substr(0,56);
    if( labels->TEST.substr(1,12) != "Start Angle:") goto L10;
    //                         Start Angle:
    readasc();
    ATHMIN=labels->TEST.substr(labels->LB2,labels->LB3-labels->LB2+1);
L20:
    getline(file4,s);
    labels->TEST = " "+s.substr(0,56);
    if(labels->TEST.substr(1,11) != "Stop Angle:")goto L20;
    //                         Stop Angle:
    readasc();
    ATHMAX=labels->TEST.substr(labels->LB2,labels->LB3-labels->LB2+1);
L30:
    getline(file4,s);
    labels->TEST = " "+s.substr(0,56);
    if(labels->TEST.substr(1,10) != "Step Size:")goto L30;
    //                         Step Size:
    readasc();
    ASTEP=labels->TEST.substr(labels->LB2,labels->LB3-labels->LB2+1);

L70:
    getline(file4,s);
    if( labels->TEST.substr(1,5) != "Range")goto L70;
    stringstream(ATHMIN) >> g1->THMIN;
    stringstream(ATHMAX) >> g1->THMAX;
    stringstream(ASTEP) >> g1->STEP;
    datax->NPTS=static_cast<int>((g1->THMAX-g1->THMIN)/g1->STEP+1.5);
    cout << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    file6 << "    DATA ID " << setw(56) << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;

    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    for (I=1; I <= datax->NPTS; ++I) file4 >> X1 >> datax->Y[I] >> X2 >> X3;
    return;
    //L99998:
    //	DBWSException("END OF FILE unit=4");
}


// SUBROUTINE TO READ SIEMENS UXD DATA FILE
void DBWS::SIEMENSREAD(void)
{
    int I;
    string s,TEST, ASTEP, ANPTS, DATAID, ATHMIN;

    //character*72 test,dataid
    //character*15 athmin,astep,anpts
    //COMMON/DATAX/Y(IDSZ),VAR(IDSZ),YC(IDSZ),KR(IDSZ),BK(IDSZ),NPTS,AMORPHOUS(IDSZ)
    //COMMON/G1/THMIN,STEP,THMAX,U,V,W,LAMDA(2),TMV,CTHM,RLIM,SBX,WDT,ULOR,VLOR,ZZZ,UC

    getline(file4,s);
    DATAID = s.substr(0,72);
L10:
    getline(file4,s);
    TEST = s.substr(0,72);
    if(TEST.substr(2,9-2+1) == "STEPSIZE")
    {
        ASTEP=TEST.substr(12,20-12+1);
        goto L10;
    }
    else if(TEST.substr(2,7-2+1) == "2THETA")
    {
        ATHMIN=TEST.substr(10,16-10+1);
        goto L10;
    }else if(TEST.substr(2,10-2+1) == "STEPCOUNT")
    {
        ANPTS=TEST.substr(13,19-13+1);
        goto L10;
    }else if(TEST.substr(2,7-2+1) == "COUNTS")
    {
        goto L20;
    }
    else
    {
        goto L10;
    }
L20:
    stringstream(ATHMIN) >> g1->THMIN;
    stringstream(ASTEP) >> g1->STEP;
    stringstream(ANPTS) >> datax->NPTS;
    g1->THMAX=g1->THMIN + g1->STEP*datax->NPTS;
    cout << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    file6 << "    DATA ID " << setw(56) << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START ="
        << setw(8) << setprecision(3) << g1->THMIN
        << ", STOP ="
        << setw(8) << setprecision(3) << g1->THMAX
        << ", STEP ="
        << setw(8) << setprecision(3) << g1->STEP << endl;
    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS << "POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    for (I=1; I <= datax->NPTS; ++I) file4 >> datax->Y[I];
    return;
    //L99998:
    //	DBWSException("END OF FILE unit=4");
    //L99999:
    //	DBWSException("IS THE FILE NOT UXD SIEMENS FORMAT?");
}

void DBWS::INPTR(void)
{
    const string UPPER = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const string LOWER = " abcdefghijklmnopqrstuvwxyz";
    // Adding the code from Ian Madsen (10 feb 98)  (Au,Cr,Fe,Co,Cu,
    //                                               Mo,Ag,Ta,W ,Au)
    const double XRYZ[10+1] = { 0.0,
        2.748510,2.289620, 1.935970, 1.788965, 1.540520 ,
        0.709260,0.559360, 0.215947, 0.209010, 0.180195 };

    int I, J, K, N,IK, KK,IX, N2X,NBC, IOF, IRH, ICY, ICZ,NBX, ITN, KKS,
        IRL, IXX, IYY, IRK, NXX, IRC,NINC,IPTS,MLTT,IXDEL,IXRAY, ITIPO,
        ISTOP,NRANGE,IIPHAS,NATOMS, LCOUNT, ISTEST;
    //IBDG
    double X,TH,ABC,THX,DIFB,TAUK,BSTEP,FLEET1, FLEET2, OFSTI0, OFSTI1,
        LAMDAM, CHMBRI,ANGMIN,ANGMAX,STPTIM;
    string SPG,DATE,DATAID,s,s1;

    IPTS = 0;


    // line 1
    getline(file5,s);
    cout << s << endl;
    char_->TITLE = s.substr(0,70);

    // line 2
    getline(file5,s);
    stringstream(s.substr( 0*4,4)) >> cntrls->JOBTYP;
    stringstream(s.substr( 1*4,4)) >> cntrls->NPROF;
    stringstream(s.substr( 2*4,4)) >> cntrls->NPHASE;
    stringstream(s.substr( 3*4,4)) >> jnk->NBCKGD;
    stringstream(s.substr( 4*4,4)) >> jnk->NEXCRG;
    stringstream(s.substr( 5*4,4)) >> jnk->NSCAT;
    stringstream(s.substr( 6*4,4)) >> cntrls->INSTRM;
    stringstream(s.substr( 7*4,4)) >> cntrls->IPREF;
    stringstream(s.substr( 8*4,4)) >> cntrls->IASYM;
    stringstream(s.substr( 9*4,4)) >> cntrls->IABSR;
    stringstream(s.substr(10*4,4)) >> cntrls->IDATA;
    stringstream(s.substr(11*4,4)) >> cntrls->ISPHASE;
    stringstream(s.substr(12*4,4)) >> cntrls->I2D94;
    //2     FORMAT(BZ,13I4)
    if (cntrls->NPROF < 0)
    {
        convert->ICNVT=1;
        file6 << "$ INPUT FILE CONVERSION FROM DBWS9411 TO DBWS9807" << endl
            << "$ CHECK ALL PARAMETERS" << endl
            << "$ SPECIAL ATTENTION REQUIRED TO ATOM MULTIPLICITIES AND OCCUPANCIES" << endl
            << "$ See USER's GUIDE,  LINE 11-41" << endl
            << "                  GOOD LUCK!" << endl;
        cout  << "$ INPUT FILE CONVERSION FROM DBWS9411 TO DBWS9807" << endl
            << "$ CHECK ALL PARAMETERS" << endl
            << "$ SPECIAL ATTENTION REQUIRED TO ATOM MULTIPLICITIES AND OCCUPANCIES" << endl
            << "$ See USER's GUIDE,  LINE 11-41" << endl
            << "                  GOOD LUCK!" << endl;
        conv94();
        goto L88088;
    }
    else
    {
        convert->ICNVT = 0;
        if (cntrls->ISPHASE > cntrls->NPHASE)
        {
            file6 << "Internal Standard Phase does not exist. Check its number in line 2, column 12." << endl
                << "No Internal Standard will be used in the QPA" << endl;
            cntrls->ISPHASE = 0;
        }
    }
    //     open file to write +/- dbws9006 & dbws9411 format (readable by ATOMS and ZORTEP)
    if(cntrls->I2D94 == 1) file53.open("ICF94.ICF");
    if (cntrls->NPROF == 9)
    {
        cntrls->NPROF = _TCHZ;
        sizestrain->NSIZESTRAIN = 9;
    }
    if (jnk->NBCKGD == -1)
    {
        codebck->IBCKCODE=jnk->NBCKGD;
        jnk->NBCKGD = 0;
        //		IBDG   = 0;
    }
    else
    {
        cntrls->FONDO  = 0;
        cntrls->IBGD   = 1;
        codebck->IBCKCODE=jnk->NBCKGD;
    }
    if(cntrls->NPHASE == 0)cntrls->NPHASE=1;
    cntrls->INSTRM=cntrls->INSTRM+1;
    cntrls->JOBTYP=cntrls->JOBTYP+1;
    //cntrls->NPROF=cntrls->NPROF+1;
    file6 << "RIETVELD ANALYSIS PROGRAM OPENDBWS," << endl
          << "COPYRIGHT 2013 BY VEGNER UTUNI." << endl << endl;
    file6 << "PROGRAM PARAMETERS:" << endl
        << "IDSZ=" << setw(5) << IDSZ
        << "    IRS=" << setw(5) << IRS
        << "    NATS=" << setw(4) << NATS
        << "     MSZ=" << setw(3) << MSZ
        << "     NOV=" << setw(5) << NOV << endl;
    file6 << char_->TITLE << endl;
    cout << "RIETVELD ANALYSIS PROGRAM OPENDBWS," << endl
         << "COPYRIGHT 2013 BY VEGNER UTUNI." << endl << endl;
    cout  << "PROGRAM PARAMETERS:" << endl
        << "IDSZ=" << setw(5) << IDSZ
        << "    IRS=" << setw(5) << IRS
        << "    NATS=" << setw(4) << NATS
        << "     MSZ=" << setw(3) << MSZ
        << "     NOV=" << setw(5) << NOV << endl;
    cout  << char_->TITLE << endl;
    if(cntrls->JOBTYP == 1) file6 << "FOR X-RAY DATA" << endl;
    if(cntrls->JOBTYP == 1 && cntrls->INSTRM == 2) file6 << "COLLECTED IN SYNCHROTRON AT NSLS OR SRS" << endl;
    if(cntrls->JOBTYP == 2) file6 << "FOR NEUTRON DATA, NUCLEAR INTENSITIES ONLY" << endl;
    if(cntrls->JOBTYP == 2 && cntrls->INSTRM == 2) file6 << "VARYING NO. OF COUNTERS AT EACH STEP" << endl;
    if(cntrls->JOBTYP == 3) file6 << "PATTERN CALCULATION,XRAY" << endl;
    if(cntrls->JOBTYP == 4) file6 << "PATTERN CALCULATION,NEUTRON" << endl;
    if(cntrls->JOBTYP < 1 || cntrls->JOBTYP > 4) DBWSException("7777");
    if(cntrls->IDATA == 0) file6 << "READ DATA IN TRADITIONAL DBWS FORMAT" << endl;
    if(cntrls->IDATA == 1) file6 << "READ DATA IN FREE FORMAT" << endl;
    if(cntrls->IDATA == 2) file6 << "READ DATA IN GSAS STD FORMAT" << endl;
    if(cntrls->IDATA == 3) file6 << "READ DATA IN PHILIPS UDF FORMAT" << endl;
    if(cntrls->IDATA == 4) file6 << "READ DATA IN SCINTAG TXT FORMAT" << endl;
    if(cntrls->IDATA == 5) file6 << "READ DATA IN SIEMENS UXD FORMAT" << endl;
    if(cntrls->IDATA == 6) file6 << "READ DATA IN RIGAKU ASC FORMAT" << endl;
    file6 << "NUMBER OF PHASES= " << setw(4) << cntrls->NPHASE << endl
        << "NUMBER OF EXCLUDED REGIONS= " << setw(4) << jnk->NEXCRG << endl
        << "NUMBER OF SCATTERING SETS= " << setw(4) << jnk->NSCAT << endl;
    if (jnk->NBCKGD-1 < 0)
    {
        goto L110;
    }
    else if(jnk->NBCKGD-1 == 0)
    {
        goto L111;
    }
    else
    {
        goto L112;
    }
L110:
    file6 << "BACKGROUND TO BE REFINED" << endl;
    goto L113;
L111:
    file6 << "BACKGROUND DATA TO BE READ FROM FILE" << endl;
    goto L113;
L112:
    file6 << "BACKGROUND CORRECTION BY INTERPOLATION BETWEEN THE " << setw(4) << jnk->NBCKGD << " POINTS GIVEN" << endl;
L113:
    if(cntrls->NPROF == _Gaussian)
    {
        file6 << "GAUSSIAN PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _Lorentzian)
    {
        file6 << "LORENTZIAN (CAUCHY) PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _Mod1)
    {
        file6 << "MOD 1 LORENTZIAN PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _Mod2)
    {
        file6 << "MOD 2 LORENTZIAN PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _SplitPearsonVII)
    {
        file6 << "SPLIT PEARSON VII PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _pseudoVoigt)
    {
        file6 << "PSEUDO-VOIGT (PV) PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _PearsonVII)
    {
        file6 << "PEARSON VII PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    else if(cntrls->NPROF == _TCHZ)
    {
        file6 << "THOMPSON-COX-HASTINGS (PV) PROFILE, NPROF = " << setw(4) << cntrls->NPROF << endl;
    }
    if (cntrls->IPREF == 0)
    {
        file6 << "IPREF=0, UDA-RIETVELD PREFERRED ORIENTATION FUNCTION" << endl;
    }
    else
    {
        file6 << "IPREF=1, MARCH-DOLLASE PREFERRED ORIENTATION FUNCTION" << endl;
    }
    //  SURFACE ROUGHNESS
    // !cp jun 97 7448 introduced line below
    if (cntrls->IABSR == 1) file6 << "IABSR=1, CORRECTION OF SURFACE ROUGHNESS BY YOUNG" << endl;
    if (cntrls->IABSR == 2) file6 << "IABSR=2, CORRECTION OF SURFACE ROUGHNESS BY, SPARKS, ET AL." << endl;
    if (cntrls->IABSR == 3) file6 << "IABSR=3, CORRECTION OF SURFACE ROUGHNESS BY SUORTTI" << endl;
    if (cntrls->IABSR == 4) file6 << "IABSR=4, CORRECTION OF SURFACE ROUGHNESS BY PITSCHKE, HERMANN, AND MATTERN" << endl;
    //  asymmetry correction  Test for asymmetry model included !cp ap 97
    if (cntrls->IASYM == 0) file6 << "IASYM=0, Usual Rietveld Asymmetry" << endl;
    if (cntrls->IASYM == 1) file6 << "IASYM=1, Asymmetry by Riello et al.:',' Powder Diffraction,10,204-206,1995" << endl;
    //-----FONDO=0 :BKG EVALUATED USING STANDARD METHODS
    //-----FONDO=1 :BKG EVALUATED USING THE ISOTROPIC THERMAL FACTORS
    //-----FONDO=2 :BKG EVALUATED USING THE OVERAL THERMAL FACTORS
    //-----AIR SCATTERING,IAXX= 1 AIR SCATTERING ADDED TO THE BACKGROUND if SCAIR<>0
    //                    IAXX= -1 AIR SCATT. EVALUATED AND SUBTRACTED FROM DATA
    //                    IAXX= 0 AIR SCATT NOT EVALUATED and ibgd = 1
    //-----LINEAR ABSORP.CORR., IAS= 1  DATA CORRECTED FOR LINEAR ABSORPTION
    //
    //-----FI = AIR FRACTION INSIDE THE SAMPLE
    if(codebck->IBCKCODE == -1)
    {
        // line 2.2
        getline(file5,s);
        stringstream(s.substr( 0,4)) >> cntrls->IAS;
        stringstream(s.substr( 4,4)) >> cntrls->FONDO;
    }
    //L5333:
    if (cntrls->IBGD == 1)
    {
        file6 << "NO AMOPHOUS AND COMPTON CORRECTION TO THE BGD" << endl;
        goto L7448;
    }
    if(cntrls->IAS == 0) file6 << "IAS = 0, NO LINEAR ABSORPTION CORRECTION" << endl;
    if(cntrls->IAS == 1) file6 << "IAS = 1, LINEAR ABSORPTION CORRECTION IS APPLIED" << endl;
    if(cntrls->FONDO == 1) file6 << "FONDO = 1,ISOTROPIC B FACTOR USED FOR BKG EVALUATION" << endl;
    if(cntrls->FONDO == 2) file6 << "FONDO = 2,OVERALL Q USED FOR BKG EVALUATION" << endl;
    // line 3
L7448:
    getline (file5,s);
    stringstream(s.substr( 0,1)) >> cntrls->IOT;
    stringstream(s.substr( 1,1)) >> cntrls->IPL;
    stringstream(s.substr( 2,1)) >> cntrls->IPC;
    stringstream(s.substr( 3,1)) >> cntrls->MAT;
    stringstream(s.substr( 4,1)) >> cntrls->NXT;

    stringstream(s.substr( 6,1)) >> cntrls->LST1;
    stringstream(s.substr( 7,1)) >> cntrls->LST2;
    stringstream(s.substr( 8,1)) >> cntrls->LST3;
    stringstream(s.substr( 9,1)) >> cntrls->IPL1;
    stringstream(s.substr(10,1)) >> cntrls->IPL2;

    stringstream(s.substr(12,1)) >> cntrls->IPLST;
    stringstream(s.substr(13,1)) >> cntrls->IPLOSS;
    stringstream(s.substr(14,1)) >> cntrls->IPLCAL;
    stringstream(s.substr(15,1)) >> cntrls->IPLPOL;
    stringstream(s.substr(16,1)) >> cntrls->IPLCOM;

    stringstream(s.substr(18,1)) >> cntrls->IPLDIS;
    stringstream(s.substr(19,1)) >> cntrls->IPLAM;
    stringstream(s.substr(20,1)) >> cntrls->IPBIG;
    if(codebck->IBCKCODE != -1)
    {
        cntrls->IPLOSS=0;
        cntrls->IPLCAL=0;
        cntrls->IPLPOL=0;
        cntrls->IPLCOM=0;
        cntrls->IPLDIS=0;
        cntrls->IPLAM =0;
        cntrls->IPBIG =0;
    }
    simoper->ISIMOP=0;
    if (cntrls->IOT != 0) file6 << "OUTPUT OBSERVED AND CALCULATED INTENSITIES ON LAST CYCLE" << endl;
    if (cntrls->IPL != 0) file6 << "GENERATE LINE PRINTER PLOT" << endl;
    if (cntrls->IPL2 != 0) file6 << "Generate file with obs, calc, dif intensiies, weighted diference, and Bragg peak positions" << endl;
    if (cntrls->IPLST == 1) file6 << "GENERATE PARAMETER LIST" << endl;
    if (cntrls->IPLST == 2) file6 << "GENERATE PARAMETER LIST" << endl << "GENERATE PARAMETERS AND STD.DEV. IN FINAL CYCLE FOR DATA BASE" << endl;
    if (cntrls->IPC == 1) file6 << "OUTPUT INTENSITIES" << endl;
    if (cntrls->IPC == 2) file6 << "OUTPUT ABSOLUTE VALUES OF STRUCTURE FACTORS + PHASE ANGLE" << endl;
    if (cntrls->IPC == 3) file6 << "OUTPUT A AND B (CALC + OBS) STRUCTURE FACTORS" << endl;
    if (cntrls->MAT != 0 && cntrls->JOBTYP < 3) file6 << "OUTPUT CORRELATION MATRIX" << endl;
    if (cntrls->NXT != 0 && cntrls->JOBTYP < 3) file6 << "GENERATE NEW INPUT FILE" << endl;
    if (cntrls->LST1 != 0) file6 << "PRINT REFLECTION LIST" << endl;
    if (cntrls->NPHASE == 1)cntrls->LST3=0;
    if (cntrls->LST3 != 0) file6 << "Print merged reflection list" << endl;
    if (cntrls->LST2 != 0) file6 << "Print corrected data" << endl;

    // line 4
    getline (file5,s);
    stringstream(s.substr( 0*8, 8)) >> g1->LAMDA[1];
    stringstream(s.substr( 1*8, 8)) >> g1->LAMDA[2];
    stringstream(s.substr( 2*8, 8)) >> params->RATIO[2];
    stringstream(s.substr( 3*8, 8)) >> g1->BKPOS;
    stringstream(s.substr( 4*8, 8)) >> g1->WDT;
    stringstream(s.substr( 5*8, 8)) >> g1->CTHM;
    stringstream(s.substr( 6*8, 8)) >> g1->TMV;
    stringstream(s.substr( 7*8, 8)) >> g1->RLIM;
    stringstream(s.substr( 8*8, 8)) >> cntrls->SW;
    //        !cp ap 21 97
    params->RATIO[1] = 1.0;
    LAMDAM=(params->RATIO[1]*g1->LAMDA[1]+params->RATIO[2]*g1->LAMDA[2])/(params->RATIO[1]+params->RATIO[2]);
    file6
        << " WAVELENGTHS= "
        << fixed
        << setw(9) << setprecision(6) << g1->LAMDA[1]
        << setw(9) << setprecision(6) << g1->LAMDA[2]
        << " LAMDA MEAN = "
        << setw(9) << setprecision(6) << LAMDAM <<endl;
    //  stop !cp ap 21 97
    file6 << "ALPHA2:ALPHA1 RATIO = " << setw(9) << setprecision(5) << params->RATIO[2] << endl;
    for (IX=1; IX <= 10; ++IX) if(1.03*g1->LAMDA[1] > XRYZ[IX])goto L119;
L119:
    IXRAY=IX;
    file6 << "BASE OF PEAK = 2.0*HW*" << setw(8) << setprecision(2) << g1->WDT << endl;
    file6 << "MONOCHROMATOR CORRECTION =" << setw(8) << setprecision(4) << g1->CTHM << endl;
    file6 << "ABSORPTION CORRECTION COEFFICIENT = " << setw(8) << setprecision(4) << g1->TMV << " CM-1" << endl
        << "SLAB-WIDTH = " << setw(8) << setprecision(4) << cntrls->SW << " CM." << endl;
    if (cntrls->IASYM == 0)
    {
        file6 << "RIETVELD ASYMMETRY CORRECTION FOR ANGLES LESS THAN "
            << setw(8) << setprecision(3) << g1->RLIM << " DEGREES" << endl;
    }
    else
    {
        file6 << "ASYMMETRY CORRECTION FOR ANGLES LESS THAN "
            << setw(8) << setprecision(3) << 90.0 - g1->RLIM << " DEGREES" << endl
            << "                         AND GREATER THAN "
            << setw(8) << setprecision(3) << 90.0 + g1->RLIM << " DEGREES" << endl;
    }

    // line 5
    getline(file5,s);
    stringstream(s.substr(0,4)) >> cntrls->MCYCLE;
    stringstream(s.substr(4,4)) >> cntrls->EPS;
    stringstream(s.substr(8,4)) >> params->RELAX[1];
    stringstream(s.substr(12,4)) >> params->RELAX[2];
    stringstream(s.substr(16,4)) >> params->RELAX[3];
    stringstream(s.substr(20,4)) >> params->RELAX[4];

    // TODO: falata codigo aqui
    stringstream(s.substr(24,8)) >> g1->THMIN;
    stringstream(s.substr(32,8)) >> g1->STEP;
    stringstream(s.substr(40,8)) >> g1->THMAX;

    if(cntrls->JOBTYP > 2)cntrls->MCYCLE=1;
    cntrls->ICYRUN = cntrls->MCYCLE;
    file6 << "NUMBER OF CYCLES = " << setw(4) << cntrls->MCYCLE << endl;
    file6 << "RELAXATION FACTORS" << endl
        << "FOR COORDINATES= " << setw(5) << setprecision(2) << params->RELAX[1] << endl
        << "FOR ANISOTRPIC TEMPERATURE FACTORS= " << setw(5) << setprecision(2) << params->RELAX[2] << endl
        << "FOR FWHM PARAMETERS= " << setw(5) << setprecision(2) << params->RELAX[3] << endl
        << "FOR LATTICE CONSTANTS= " << setw(5) << setprecision(2) << params->RELAX[4] << endl;
    file6 << "EPS-VALUE= " << setw(6) << setprecision(1) << cntrls->EPS << endl;
    if(jnk->NBCKGD < 2)goto L120;

    // line 6(*)
    for (I=1; I <= jnk->NBCKGD; ++I)
    {
        getline(file5,s);
        stringstream(s.substr(0,8)) >> jnk->POS[I];
        stringstream(s.substr(8,8)) >> jnk->BCK[I];
    }
    file6 << "BACKGROUND" << endl
        << "POSITION    INTENSITY" << endl;
    for (I=1; I <= jnk->NBCKGD; ++I)
    {
        file6 << setw(9) << setprecision(4) << jnk->POS[I]
        << setw(9) << setprecision(4) << jnk->BCK[I] << endl;
    }
L120:
    if(jnk->NEXCRG <= 0)goto L122;
    // line 7(*)
    for (I=1; I <= jnk->NEXCRG; ++I)
    {
        getline(file5,s);
        stringstream(s.substr(0,8)) >> jnk->ALOW[I];
        stringstream(s.substr(8,8)) >> jnk->AHIGH[I];
    }
    file6 << "EXCLUDED REGIONS" << endl
        << "FROM     TO" << endl;
    for (I=1; I <= jnk->NEXCRG; ++I)
    {
        file6 << setw(9) << setprecision(4) << jnk->ALOW[I]
        << setw(9) << setprecision(4) << jnk->AHIGH[I] << endl;
    }
L122:
    if(jnk->NSCAT <= 0)goto L124;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        if(cntrls->JOBTYP == 2 || cntrls->JOBTYP == 4)
        {
            // line 8.1 XRD(*)
            getline(file5,s);
            coefc->NAM[I] = s.substr(0,4);
            stringstream(s.substr(4,8)) >> coeff->DFP[I];
            stringstream(s.substr(12,8)) >> coeff->XMAS[I];
            goto L125;
        }
        // line 8.1 ND(*)
        getline(file5,s);
        coefc->NAM[I] = s.substr(0,4);
        stringstream(s.substr(4,8)) >> coeff->DFP[I];
        stringstream(s.substr(12,8)) >> coeff->DFPP[I];
        stringstream(s.substr(20,8)) >> coeff->XMAS[I];
        K=0;
        // line 8.2 XRD(*)
L126:
        getline(file5,s);
        for (J=1; J <= 9; ++J) stringstream(s.substr((J-1)*8,8)) >> coeff->AC[J][I];
        if (coeff->AC[1][I] == -100.0) coeff->COEF(&I,&K);
        if(coeff->AC[3][I] != 0.)goto L125;
        K=K+1;
        coeff->POSI[K]=coeff->AC[1][I];
        coeff->SCAT[K]=coeff->AC[2][I];
        if (K <= 29) goto L126;
        file6 << "TOO MANY SCATTERING TABLE ENTRIES" << endl;
        DBWSException("7700");
L125:;
    }
    if(cntrls->JOBTYP == 1 || cntrls->JOBTYP == 3)goto L129;
    file6 << "SCATTERING LENGTHS" << endl;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        file6 << "FOR " << setw(4) << coefc->NAM[I] << "     "
            << setw(10) << setprecision(6) << coeff->DFP[I] << endl;
    }
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        for (J=1; J <= 9; ++J) coeff->AC[J][I]=0.0;
        coeff->DFPP[I]=0.0;
    }
    goto L124;
L129:
    file6 << "FORMFACTORS" << endl;
    for (I=1; I <= jnk->NSCAT; ++I)
    {
        file6 << "FOR " << setw(4) << coefc->NAM[I]
        << " DFP=" << setw(10) << setprecision(6) << coeff->DFP[I]
        << " DFPP=" << setw(10) << setprecision(6) << coeff->DFPP[I] << endl
            << "COEFFICIENTS= ";
        for (J=1; J <= 9; ++J) file6 << setw(10) << setprecision(6) << coeff->AC[J][I];
        file6 << endl;
    }
L124:

    // line 9
    getline(file5,s);
    stringstream(s.substr(0,8)) >> cntrls->MAXS;
    if(cntrls->MAXS > MSZ)
    {
        file6 << "* YOU HAVE DECLARED MORE CODEWORDS THAN WILL FIT INTO *" << endl
            << "* THE -MSZ- ARRAY.  EITHER DECREASE THE # OF CODEWORD *" << endl
            << "* OR  INCREASE  THE  -MSZ-  ARRAY  SIZE AND RECOMPILE *" << endl;
        cout  << "* YOU HAVE DECLARED MORE CODEWORDS THAN WILL FIT INTO *" << endl
            << "* THE -MSZ- ARRAY.  EITHER DECREASE THE # OF CODEWORD *" << endl
            << "* OR  INCREASE  THE  -MSZ-  ARRAY  SIZE AND RECOMPILE *" << endl;
        goto L99995;
    }
    cout << "INPUT:    CYCLES =" << setw(4) << cntrls->MCYCLE << "     REFINABLE PARAMETERS =" << setw(4) <<cntrls->MAXS << endl;
    //     CHECK DIMENSIONING FOR SOME EQUIVALENCED ARRAYS
    if (IDSZ < MSZ*cntrls->MAXS)
    {
        file6 << "CHANGE IDSZ OR MSZ SO THAT IDSZ IS EQUAL TO OR GREATER THAN MSZ*MAXS" << endl
            << "IDSZ, MAXIMUM NO. OF DATA POINTS = " << setw(6) << IDSZ << endl
            << "MSZ,  MATRIX SIZE                = " << setw(6) << MSZ << endl
            << "MAXS, NO. OF PARAMETERS VARIED   = " << setw(6) << endl
            << "*** JUST A DIMENSIONING ERROR ***" << endl;
        file7 << "CHANGE IDSZ OR MSZ SO THAT IDSZ IS EQUAL TO OR GREATER THAN MSZ*MAXS" << endl
            << "IDSZ, MAXIMUM NO. OF DATA POINTS = " << setw(6) << IDSZ << endl
            << "MSZ,  MATRIX SIZE                = " << setw(6) << MSZ << endl
            << "MAXS, NO. OF PARAMETERS VARIED   = " << setw(6) << endl
            << "*** JUST A DIMENSIONING ERROR ***" << endl;
        DBWSException("IDSZ IS LESS THAN MSZ*MAXS");
    }
    if (cntrls->JOBTYP > 2) cntrls->MAXS=0;
    file6 << "NUMBER OF PARAMETERS VARIED= " << setw(5) << cntrls->MAXS << endl;

    // line 10.1
    getline(file5,s);
    stringstream(s.substr(0,8))   >> params->GLB[1];
    stringstream(s.substr(1*8,8)) >> params->GLB[10];
    stringstream(s.substr(2*8,8)) >> params->GLB[11];
    stringstream(s.substr(3*8,8)) >> params->GLB[8];
    stringstream(s.substr(4*8,8)) >> params->GLB[9];
    stringstream(s.substr(5*8,8)) >> params->GLB[12];
    stringstream(s.substr(6*8,8)) >> params->GLB[13];

    // line 10.11
    getline(file5,s);
    stringstream(s.substr(0,8))   >> params->AGLB[1];
    stringstream(s.substr(1*8,8)) >> params->AGLB[10];
    stringstream(s.substr(2*8,8)) >> params->AGLB[11];
    stringstream(s.substr(3*8,8)) >> params->AGLB[8];
    stringstream(s.substr(4*8,8)) >> params->AGLB[9];
    stringstream(s.substr(5*8,8)) >> params->AGLB[12];
    stringstream(s.substr(6*8,8)) >> params->AGLB[13];

    file6 << "GLOBAL PARAMETERS AND CODEWORDS" << endl
        << "ZEROPOINT= "
        << setw(8) << setprecision(2) << params->GLB[1]
    << setw(8) << setprecision(2) << params->AGLB[1] << endl;

    //-----PMON1,PMON2=PARAMETER OF THE MONOCHROMATOR
    //-----IT READS PARAMETER OF THE MONOCHROMATOR AND AIR SCALE
    //-----if THE MONOCHROMATOR WORKS ON THE INCIDENT BEAM PUT :
    //-----PMON1=1;PMON2=0;
    // next 2 'READ' are for amorphous bkg codes
    // SCAIR=glb(17), FLSCAIR=aglb(17), SCAM =params->GLB[20], FLSCAM=params->AGLB[20]
    // PMON1=params->GLB[18], FLMON1 =params->AGLB[18], PMON2=params->GLB[19], FLMON2=params->AGLB[19]
    //      READ(5,455,END=99999)SCAIR,FLSCAIR,SCAM,FLSCAM,
    //     *  PMON1,FLMON1,PMON2,FLMON2
    //
    //      READ(5,455,END=99999)glb(17),aglb(17),params->GLB[20], params->AGLB[20],
    //     *  params->GLB[18],params->AGLB[18],params->GLB[19], params->AGLB[19]
    //455     FORMAT(BZ,8F8.0)
    //  !cp jun 95 start
    if (cntrls->IBGD == 1) goto L4555;

    // line 10.2 and line 10.21
    getline(file5,s);
    stringstream(s.substr(0*8,8)) >> params->GLB[20];
    stringstream(s.substr(1*8,8)) >> params->GLB[18];
    stringstream(s.substr(2*8,8)) >> params->GLB[19];

    getline(file5,s);
    stringstream(s.substr(0*8,8)) >> params->AGLB[20];
    stringstream(s.substr(1*8,8)) >> params->AGLB[18];
    stringstream(s.substr(2*8,8)) >> params->AGLB[19];

    file6 << "AMORPHOUS SCALE and CODEWORD= "
        << setw(14) << setprecision(4) << params->GLB[20]
    << setw(14) << setprecision(4) << params->AGLB[20] << endl;
    file6 << "MONOCROMATOR BANDPASS PARAMETERS AND CODEWORDS" << endl
        << "PARAMETERS MONOC="
        << setw(8) << setprecision(4) << params->GLB[18]
    << setw(8) << setprecision(4) << params->AGLB[18]
    << "                  "
        << setw(8) << setprecision(4) << params->GLB[19]
    << setw(8) << setprecision(4) << params->AGLB[19] << endl;
L4555:
    if(jnk->NBCKGD != 0)goto L49;
    // line 10.4(*)
    // line 10.3 and line 10.31
    getline(file5,s);
    stringstream(s.substr(0*9,9)) >> params->GLB[2];
    stringstream(s.substr(1*9,9)) >> params->GLB[3];
    stringstream(s.substr(2*9,9)) >> params->GLB[4];
    stringstream(s.substr(3*9,9)) >> params->GLB[5];
    stringstream(s.substr(4*9,9)) >> params->GLB[6];
    stringstream(s.substr(5*9,9)) >> params->GLB[7];

    getline(file5,s);
    stringstream(s.substr(0*9,9)) >> params->AGLB[2];
    stringstream(s.substr(1*9,9)) >> params->AGLB[3];
    stringstream(s.substr(2*9,9)) >> params->AGLB[4];
    stringstream(s.substr(3*9,9)) >> params->AGLB[5];
    stringstream(s.substr(4*9,9)) >> params->AGLB[6];
    stringstream(s.substr(5*9,9)) >> params->AGLB[7];


    file6 << "BACKGROUND PARAMETERS AND CODEWORDS" << endl
        << "ORIGIN OF BACKGROUND POLYNOMIAL AT TWO-THETA = "
        << setw(8) << setprecision(3) << g1->BKPOS << "DEGREES" << endl
        << setw(12) << setprecision(4) << params->GLB[2]
        << setw(12) << setprecision(4) << params->GLB[3]
        << setw(12) << setprecision(4) << params->GLB[4]
        << setw(12) << setprecision(4) << params->GLB[5]
        << setw(12) << setprecision(4) << params->GLB[6]
        << setw(12) << setprecision(4) << params->GLB[7] << endl
        << setw(12) << setprecision(3) << params->AGLB[2] << "    "
        << setw(12) << setprecision(3) << params->AGLB[3] << "    "
        << setw(12) << setprecision(3) << params->AGLB[4] << "    "
        << setw(12) << setprecision(3) << params->AGLB[5] << "    "
        << setw(12) << setprecision(3) << params->AGLB[6] << "    "
        << setw(12) << setprecision(3) << params->AGLB[7] << "    " << endl;
L49:
    file6 << "DISPLACEMENT PEAKSHIFT PARAMETER AND CODEWORD"
        << setw(8) << setprecision(2) << params->GLB[10]
    << setw(8) << setprecision(2) << params->AGLB[10] << endl
        << "TRANSPARENCY PEAKSHIFT PARAMETER AND CODEWORD"
        << setw(8) << setprecision(2) << params->GLB[11]
    << setw(8) << setprecision(2) << params->AGLB[11] << endl;
    file6 << "SURFACE ROUGHNESS P PARAMETER AND CODEWORD"
        << setw(9) << setprecision(4) << params->GLB[8]
    << setw(9) << setprecision(4) << params->AGLB[8] << endl
        << "SURFACE ROUGHNESS Q PARAMETER AND CODEWORD"
        << setw(9) << setprecision(4) << params->GLB[9]
    << setw(9) << setprecision(4) << params->AGLB[9] << endl
        << "SURFACE ROUGHNESS R PARAMETER AND CODEWORD"
        << setw(9) << setprecision(4) << params->GLB[12]
    << setw(9) << setprecision(4) << params->AGLB[12] << endl
        << "SURFACE ROUGHNESS T PARAMETER AND CODEWORD"
        << setw(9) << setprecision(4) << params->GLB[13]
    << setw(9) << setprecision(4) << params->AGLB[13] << endl;
    if(cntrls->JOBTYP > 2)goto L483;
    //-----if PATTERN CALCULATION ONLY FOR SYNCHROTRON X-RAY DATA goto 501
    if(cntrls->JOBTYP == 1 && cntrls->INSTRM == 2) goto L501;
    //-----if PATTERN CALCULATION ONLY FOR MULTIPLE NEUTRON DATA goto 601
    if(cntrls->JOBTYP == 2 && cntrls->INSTRM == 2) goto L601;
    //471    format(BZ,F8.5,f8.7,f8.5,A56)
    // read DBWS formated
    if(cntrls->IDATA == 0 || cntrls->IDATA == 1)
    {
        getline(file4,s);
        stringstream(s.substr(0*8,8)) >> g1->THMIN;
        stringstream(s.substr(1*8,8)) >> g1->STEP;
        stringstream(s.substr(2*8,8)) >> g1->THMAX;
        DATAID = s.substr(3*8,56);
        cout << "DATA RANGE (2THETA):  START =" << setw(8) << setprecision(3) << g1->THMIN
            << ", STOP =" << setw(8) << setprecision(3) << g1->THMAX
            << ", STEP =" << setw(8) << setprecision(3) << g1->STEP << endl;
        file6 << "    DATA ID " << DATAID << endl;
        cout << "DATA RANGE (2THETA):  START =" << setw(8) << setprecision(3) << g1->THMIN
            << ", STOP =" << setw(8) << setprecision(3) << g1->THMAX
            << ", STEP =" << setw(8) << setprecision(3) << g1->STEP << endl;
        datax->NPTS=static_cast<int>((g1->THMAX-g1->THMIN)/g1->STEP+1.5);
        if (datax->NPTS > IDSZ)
        {
            file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << " POINTS" << endl
                << setw(5) << datax->NPTS << " POINTS WERE INPUT" << endl
                << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
            file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << " POINTS" << endl
                << setw(5) << datax->NPTS << " POINTS WERE INPUT" << endl
                << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
            cout  << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << " POINTS" << endl
                << setw(5) << datax->NPTS << " POINTS WERE INPUT" << endl
                << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
            DBWSException("TOO MANY DATA POINTS");
        }
    }
    else if(cntrls->IDATA == 2)
    {
        // read GSAS data file (read start,stop,step and data)
        GSASREAD();
        goto L3333;
    }
    if(cntrls->IDATA == 3)
    {
        // read Philips data file (read start,stop,step and data)
        PHILIPSREAD();
        goto L3333;
    }
    if(cntrls->IDATA == 4)
    {
        // read SCINTAG data file (read start,stop,step and data)
        scintag();
        goto L3333;
    }
    if(cntrls->IDATA == 5)
    {
        // read  SIEMENS UXD data file (read start,stepsize,stepcount and data)
        SIEMENSREAD();
        goto L3333;
    }
    if(cntrls->IDATA == 6)
    {
        // read  RIGAKU data file (read start,stepsize,stop,stepcount and data)
        rigakuread();
        goto L3333;
    }
    // END SPECIAL FORMATS DATA FILE
    if(cntrls->IDATA == 0)
    {
        I=0;
        while ( I < datax->NPTS)
        {
            getline(file4,s);
            for (J = 1; J <= 8; ++J)
            {
                s1 = s.substr((J-1)*8,7);
                if ( !s1.empty() )
                {
                    ++I;
                    stringstream(s1) >> datax->Y[I];
                }
            }
        }
    }
    if(cntrls->IDATA == 1)
    {
        for (I=1; I <= datax->NPTS; ++I) file4 >> datax->Y[I];
    }

    //     CHECK FOR NON-ZERO COUNTS AT ALL POINTS
    maxint->XMAXINT = 0.0;
L3333:
    for (I=1; I <= datax->NPTS; ++I)
    {
        if(datax->Y[I] <= 1.0E-6) datax->Y[I]=1.0;
        if(datax->Y[I] > maxint->XMAXINT) maxint->XMAXINT=datax->Y[I];
    }
    for (I=1; I <= datax->NPTS; ++I)
    {
        //     BUILD UP AMORPHOUS-VECTOR
        //     if REQUIRED MAKE ABSORPTION CORRECTION
        //-----COMPUTE TWO-THETA
        TH = g1->THMIN + static_cast<double>(I-1) * g1->STEP;
        datax->VAR[I]=datax->Y[I];
        //-----COMPUTE SAMPLE ABSORPTION CORRECTION
        if(cntrls->IAS == 1)
        {
            ABSORP(g1->TMV,cntrls->SW,TH,&ABC);
            datax->Y[I] = datax->Y[I] / ABC;
            datax->VAR[I]=datax->VAR[I]/ABC;
        }
    }
    goto L484;
    //       READ DATA FROM SYNCHROTRON SOURCE AND CORRECT DATA FOR DEAD TIME,
    //      CALCULATE VARIANCE FOR EACH OF THE DATA POINTS
    //     DATE IS OCT85,FEB86,AUG86,SRS83,SRS91
    //     NRANGE IS THE NO OF BLOCKS IN WHICH THE DATA ARE GIVEN
    //     OFSTI0 - DARK BEAM CURRENT, READING WITH NO ELECTRONS IN THE CHAMBER
    //     OFSTI1 - DETECTOR DARK BEAM CURRENT
    //     CHMBRI - ALL CURRENTS ARE  NORMALISED TO CHMBRI
L501:
    getline(file4,s);
    DATE = s.substr(0,5);
    DATAID = s.substr(5,56);
    file6 << "    DATA ID " << DATAID << endl;
    getline(file4,s);
    stringstream(s.substr(0*8,8)) >> NRANGE;
    stringstream(s.substr(1*8,8)) >> CHMBRI;
    stringstream(s.substr(2*8,10)) >> TAUK;

    //     NPTS IS THE COUNTER FOR TOTAL NO OF POINTS IN ALL THE RANGES
    datax->NPTS = 0;
    for (J=1; J <= NRANGE; ++J)
    {
        //     READ INFORMATION ABOUT EACH RANGE
        getline(file4,s);
        stringstream(s.substr(0*8,8)) >> ANGMIN;
        stringstream(s.substr(1*8,8)) >> g1->STEP;
        stringstream(s.substr(2*8,8)) >> ANGMAX;
        stringstream(s.substr(3*8,8)) >> STPTIM;
        stringstream(s.substr(4*8,8)) >> OFSTI0;
        stringstream(s.substr(5*8,8)) >> OFSTI1;
        IPTS = static_cast<int>((ANGMAX-ANGMIN)/g1->STEP +1.5);
        //     FIND MAXIMUM AND MINIMUM TWO THETA IN ALL RANGES
        if (J == 1) g1->THMIN = ANGMIN;
        if (J == NRANGE) g1->THMAX = ANGMAX;
        if (J > 1)
        {
            getline(file4,s);
            IPTS = IPTS - 1;
        }
        if (datax->NPTS+IPTS > IDSZ)
        {
            file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
                << setw(5) << (datax->NPTS+IPTS) << " POINTS WERE INPUT" << endl
                << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
            file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
                << setw(5) << (datax->NPTS+IPTS) << " POINTS WERE INPUT" << endl
                << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
            DBWSException("TOO MANY DATA POINTS");
        }
        //     VAR(I) IS USED FOR TWO QUANTITIES, JUST TO SAVE SOME SPACE.
        if (DATE == "OCT85")
        {
            for (I=1; I <= IPTS; ++I)
            {
                getline(file4,s);
                stringstream(s.substr(24,7)) >> datax->Y[I+datax->NPTS];
                stringstream(s.substr(32,7)) >> datax->VAR[I+datax->NPTS];
            }
        }
        else if (DATE == "FEB86")
        {
            for (I=1; I <= IPTS; ++I)
            {
                getline(file4,s);
                stringstream(s.substr(24,7)) >> datax->Y[I+datax->NPTS];
                stringstream(s.substr(32,7)) >> datax->VAR[I+datax->NPTS];
            }
        }
        else if (DATE == "AUG86")
        {
            for (I=1; I <= IPTS; ++I)
            {
                getline(file4,s);
                stringstream(s.substr(51,10)) >> datax->VAR[I+datax->NPTS];
                stringstream(s.substr(62,10)) >> datax->Y[I+datax->NPTS];
            }
        }
        else if (DATE == "SRS83")
        {
            for (I=1; I <= IPTS; ++I)
            {
                getline(file4,s);
                stringstream(s.substr(37,9)) >> datax->VAR[I+datax->NPTS];
                stringstream(s.substr(47,9)) >> datax->Y[I+datax->NPTS];
            }
        }
        else if (DATE == "SRS91")
        {
            for (I=1; I <= IPTS; ++I)
            {
                getline(file4,s);
                stringstream(s.substr(29,9)) >> datax->VAR[I+datax->NPTS];
                stringstream(s.substr(48,9)) >> datax->Y[I+datax->NPTS];
            }
        }
        else
        {
            file6 << "    WHEN AND WHERE WERE THESE DATA TAKEN. OCT85,FEB86,AUG86,SRS83,SRS91" << endl;
        }
        for (I=datax->NPTS+1; I <= IPTS+datax->NPTS; ++I)
        {
            datax->Y[I] = datax->Y[I]/STPTIM;
            FLEET1 = datax->VAR[I]-OFSTI0;
            FLEET2= FLEET1/CHMBRI;
            FLEET2= pow((FLEET2*(1-TAUK*datax->Y[I])) , 2.0);
            datax->VAR[I] = (STPTIM*datax->Y[I])/FLEET2;
            datax->Y[I] = STPTIM*(datax->Y[I]/(1-TAUK*datax->Y[I])-OFSTI1)*CHMBRI/FLEET1;
            if (abs(datax->Y[I]) < 0.01) datax->Y[I]=1.0;
        }
        //L530:
        datax->NPTS=datax->NPTS+IPTS;
    }
    file6 << "DATA RANGE (2THETA):  START = " << setw(8) << setprecision(2) << g1->THMIN
        << ", STOP =" << setw(8) << setprecision(2) << g1->THMAX
        << ", STEP =" << setw(8) << setprecision(2) << g1->STEP << endl;
    cout  << "DATA RANGE (2THETA):  START = " << setw(8) << setprecision(2) << g1->THMIN
        << ", STOP =" << setw(8) << setprecision(2) << g1->THMAX
        << ", STEP =" << setw(8) << setprecision(2) << g1->STEP << endl;
    goto L484;
    //       ENDS SYNCHROTRON DATA MODifICATION

    //     BEGIN VARIANCE CALCULATION FOR VARYING NO. OF COUNTERS AT EACH STEP
L601:
    getline(file4,s);
    stringstream(s.substr(0*8,8)) >> g1->THMIN;
    stringstream(s.substr(1*8,8)) >> g1->STEP;
    stringstream(s.substr(2*8,8)) >> g1->THMAX;
    DATAID = s.substr(3*8,56);
    file6 << "    DATA ID " << DATAID << endl;
    file6 << "DATA RANGE (2THETA):  START =" << setw(8) << setprecision(2) << g1->THMIN
        << ", STOP =" << setw(8) << setprecision(2) << g1->THMAX
        << ", STEP =" << setw(8) << setprecision(2) << g1->STEP << endl;
    datax->NPTS = static_cast<int>((g1->THMAX-g1->THMIN)/g1->STEP+1.5);
    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << datax->NPTS+IPTS << " POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << datax->NPTS+IPTS << " POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
    I=0;
    while ( I > datax->NPTS)
    {
        getline(file4,s);
        for (J = 1; J <= 10; ++J)
        {
            s1 = s.substr((J-1)*8,2);
            if ( !s1.empty() )
            {
                ++I;
                stringstream(s1) >> datax->VAR[I];
            }
            s1 = s.substr((J-1)*8+2,6);
            if ( !s1.empty() )
            {
                stringstream(s1) >> datax->Y[I];
            }
        }
    }
    for (I=1; I <= datax->NPTS; ++I) datax->VAR[I]=datax->Y[I]/datax->VAR[I];
    goto L484;
L483:
    datax->NPTS = static_cast<int>((g1->THMAX-g1->THMIN)/g1->STEP+1.5);
    if (datax->NPTS > IDSZ)
    {
        file6 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS+IPTS << " POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        file7 << "PROGRAM CAN HANDLE " << setw(5) << IDSZ << "POINTS" << endl
            << setw(5) << datax->NPTS+IPTS << " POINTS WERE INPUT" << endl
            << "INCREASE IDSZ IN PARAMETER STATEMENT" << endl;
        DBWSException("TOO MANY DATA POINTS");
    }
L484:
    if (jnk->NBCKGD-1 < 0)
    {
        goto L474;
    }
    else if (jnk->NBCKGD-1 == 0)
    {
        goto L475;
    }
    else
    {
        goto L476;
    }
L474:
    TH=g1->THMIN-g1->STEP;
    for (I=1; I <= datax->NPTS; ++I)
    {
        TH=TH+g1->STEP;
        THX=TH/g1->BKPOS-1.0;
        datax->BK[I]=params->GLB[2];
        for (J=2; J <= 6; ++J) datax->BK[I]=datax->BK[I] + params->GLB[J+1] * pow(THX,J-1);
    }
    goto L477;
L475:
    file3.open(file3name.data());
    I=0;
    while ( I > datax->NPTS)
    {
        getline(file3,s);
        for (J = 1; J <= 8; ++J)
        {
            s1 = s.substr((J-1)*8,7);
            if ( !s1.empty() )
            {
                ++I;
                stringstream(s1) >> datax->BK[I];
            }
        }
    }
    goto L477;
L476:
    DIFB=jnk->POS[1]-g1->THMIN;
    if (DIFB < 0.0) goto L4760; else goto L4761;
L4761:
    NBX=static_cast<int>(DIFB/g1->STEP+2.5);
    for (I=1; I <= NBX; ++I) datax->BK[I]=jnk->BCK[1];
    NXX=1;
    goto L4764;
L4760:
    NBX=static_cast<int>((jnk->POS[2]-g1->THMIN)/g1->STEP+1.5);
    datax->BK[1]=jnk->BCK[1]-DIFB/(jnk->POS[2]-jnk->POS[1])*(jnk->BCK[2]-jnk->BCK[1]);
    BSTEP=g1->STEP/(jnk->POS[2]-jnk->POS[1])*(jnk->BCK[2]-jnk->BCK[1]);
    for (I=2; I <= NBX; ++I) datax->BK[I]=datax->BK[I-1]+BSTEP;
    NXX=2;
L4764:
    NBC=jnk->NBCKGD-1;
    if(jnk->POS[jnk->NBCKGD] >= g1->THMAX)goto L4767;
    jnk->POS[jnk->NBCKGD+1]=g1->THMAX;
    jnk->BCK[jnk->NBCKGD+1]=jnk->BCK[jnk->NBCKGD];
    NBC=NBC+1;
L4767:
    for (J=NXX; J <= NBC; ++J)
    {
        BSTEP=g1->STEP*(jnk->BCK[J+1]-jnk->BCK[J])/(jnk->POS[J+1]-jnk->POS[J]);
        NINC=static_cast<int>((jnk->POS[J+1]-jnk->POS[J])/g1->STEP+1.5);
        N2X=min(datax->NPTS,NBX+NINC);
        for (I=NBX; I <= N2X; ++I) datax->BK[I]=datax->BK[I-1]+BSTEP;
        NBX=N2X;
        if(NBX == datax->NPTS)goto L477;
    }
L477:
    for (K=1; K <= cntrls->NPHASE; ++K) refls->ICR[K]=0;


    // start loop on phase
    for (K=1; K <= cntrls->NPHASE; ++K)
    {
        // line 11.1
        getline(file5,s);
        char_->PHSNM[K] = s.substr(0,50);
        file6 << "PHASE " << setw(2) << K << endl << char_->PHSNM[K] << endl;

        // line 11.2
        getline(file5,s);
        stringstream(s.substr(0,4)) >> jnk->NATOM[K];
        stringstream(s.substr(4,4)) >> jnk->NMOL[K];
        stringstream(s.substr(8,7)) >> multip->SAQF[K];
        stringstream(s.substr(16,4)) >> jnk->PREF[K][1];
        stringstream(s.substr(20,4)) >> jnk->PREF[K][2];
        stringstream(s.substr(24,4)) >> jnk->PREF[K][3];
        stringstream(s.substr(28,7)) >> multip->WTIS[K];
        N=jnk->NATOM[K];
        if (multip->SAQF[K] <= 0.0)
        {
            multip->SAQF[K]=1.0;
            cout << "Particle absorption factor (phase " << setw(2) << K << ") is now 1." << endl;
        }
        ISTEST = cntrls->ISPHASE;
        if (K == cntrls->ISPHASE)
        {
            if (multip->WTIS[K] == 0.0)
            {
                cntrls->ISPHASE = 0;
                cout << "WARNING: Internal Standard WT% = ZERO. Check ISWT in line 11.2 for phase "
                    << setw(2) << ISTEST << "." << endl
                    << "ISPHASE (line 2) turned to ZERO. Amourphous content will not be calculated." << endl;
                file6 << "               WARNING: Wt% for internal Standard is ZERO." << endl
                    << "               Check ISWT in line 11.2 for this phase." << endl
                    << "               ISPHASE (line 2) turned to ZERO." << endl
                    << "               AMORPHOUS CONTENT WILL NOT BE CALCULATED." << endl;
            }
        }
        if (K == cntrls->ISPHASE)
        {
            file6 << "NUMBER OF ATOMS= " << setw(4) << N << endl
                << "NUMBER OF FORMULA UNITS PER UNIT CELL= " << setw(4) << jnk->NMOL[K] << endl
                << "PARTICLE ABSORPTION FACTOR = " << setw(8) << setprecision(4) << multip->SAQF[K] << endl
                << "PREFERRED ORIENTATION VECTOR= " << setw(8) << setprecision(4) << jnk->PREF[K][1] << setw(8) << setprecision(4) << jnk->PREF[K][2] << setw(8) << setprecision(4) << jnk->PREF[K][3] << endl
                << "MASS% IN THE SAMPLE= " << setw(7) << setprecision(2) << multip->WTIS[K] << endl;
        }
        else
        {
            file6 << "NUMBER OF ATOMS= " << setw(4) << N << endl
                << "NUMBER OF FORMULA UNITS PER UNIT CELL= " << setw(4) << jnk->NMOL[K] << endl
                << "PARTICLE ABSORPTION FACTOR = " << setw(8) << setprecision(4) << multip->SAQF[K] << endl
                << "PREFERRED ORIENTATION VECTOR= " << setw(8) << setprecision(4) << jnk->PREF[K][1] << setw(8) << setprecision(4) << jnk->PREF[K][2] << setw(8) << setprecision(4) << jnk->PREF[K][3] << endl;
        }

        // line 11.3
        getline(file5,s);
        char_->SYMB[K] = s.substr(0,20);

        SPG = " " + char_->SYMB[K];
        for (I=1; I <= 20; ++I)
        {
            //C convert lower case to upper case
            for (IK=1; IK <= 26; ++IK) if (SPG[I] == LOWER[IK]) SPG[I]=UPPER[IK];
            // finish conversion
        }
        if (convert->ICNVT != 1) file6 << "THE SPACE GROUP IS " << SPG << endl;
        spgcom->SPGP(SPG);
        // getting multiplicity of each phase !cp jun 96)
        simoper->ISIMOP=1;
        RTMT(&cntrls->IPL1,&K);
        multip->XMLTP[K]=multip->MLTPHASE;
        file6 << "The multiplicity of the general site is " << setw(3) << multip->MLTPHASE << endl;
        //-----READ AND PRINT FOR EACH ATOM
        IOF=0;
        if(K > 1)
        {
            for (IIPHAS=2; IIPHAS <= K; ++IIPHAS) IOF = IOF + jnk->NATOM[IIPHAS-1];
        }
        file6 << "***INITIAL PARAMETERS***" << endl
            << "ATOM    M NTYP              X         Y         Z         B        So" << endl
            << "                          B11       B22       B33       B12       B13       B23" << endl;

        // line 11-4i
        // READ and WRITE, and respectives FORMAT command lines below were
        // changed to incorporate the parameter MURT(I+IOF) * !cp jun 96
        for (I=1; I <= N; ++I)
        {
            getline(file5,s);
            parac->ATEXT[I+IOF] = s.substr(0,4);
            stringstream(s.substr(5,4)) >> multip->MURT[I+IOF];
            parac->NTYP[I+IOF] = s.substr(10,4);
            stringstream(s.substr(16,8)) >> params->XL[I+IOF][1];
            stringstream(s.substr(24,8)) >> params->XL[I+IOF][2];
            stringstream(s.substr(32,8)) >> params->XL[I+IOF][3];
            stringstream(s.substr(40,8)) >> params->XL[I+IOF][4];
            stringstream(s.substr(48,8)) >> params->XL[I+IOF][5];

            getline(file5,s);
            stringstream(s.substr(16,8)) >> params->A[I+IOF][1];
            stringstream(s.substr(24,8)) >> params->A[I+IOF][2];
            stringstream(s.substr(32,8)) >> params->A[I+IOF][3];
            stringstream(s.substr(40,8)) >> params->A[I+IOF][4];
            stringstream(s.substr(48,8)) >> params->A[I+IOF][5];

            getline(file5,s);
            stringstream(s.substr(0,8)) >> params->XL[I+IOF][6];
            stringstream(s.substr(8,8)) >> params->XL[I+IOF][7];
            stringstream(s.substr(16,8)) >> params->XL[I+IOF][8];
            stringstream(s.substr(24,8)) >> params->XL[I+IOF][9];
            stringstream(s.substr(32,8)) >> params->XL[I+IOF][10];
            stringstream(s.substr(40,8)) >> params->XL[I+IOF][11];

            getline(file5,s);
            stringstream(s.substr(0,8)) >> params->A[I+IOF][6];
            stringstream(s.substr(8,8)) >> params->A[I+IOF][7];
            stringstream(s.substr(16,8)) >> params->A[I+IOF][8];
            stringstream(s.substr(24,8)) >> params->A[I+IOF][9];
            stringstream(s.substr(32,8)) >> params->A[I+IOF][10];
            stringstream(s.substr(40,8)) >> params->A[I+IOF][11];
        }
        // convert lower case to upper case
        for (ITN = 1; ITN <= N; ++ITN)
        {
            for (ITIPO = 1; ITIPO <= 2; ++ITIPO)
            {
                for (IK=1; IK <= 26; ++IK) if (parac->NTYP[ITN+IOF][ITIPO] == LOWER[IK]) parac->NTYP[ITN+IOF][ITIPO]=UPPER[IK];
            }
        }
        // finish conversion
        for (I=1; I <= N; ++I)
        {
            file6
                << setw(4) << parac->ATEXT[I+IOF] << " " << setw(4) << multip->MURT[I+IOF]
                << " " << parac->NTYP[I+IOF] << "        "
                << setw(10) << setprecision(5) << params->XL[I+IOF][1]
                << setw(10) << setprecision(5) << params->XL[I+IOF][2]
                << setw(10) << setprecision(5) << params->XL[I+IOF][3]
                << setw(10) << setprecision(5) << params->XL[I+IOF][4]
                << setw(10) << setprecision(5) << params->XL[I+IOF][5] << endl
                << "                      "
                << setw(10) << setprecision(5) << params->XL[I+IOF][6]
                << setw(10) << setprecision(5) << params->XL[I+IOF][7]
                << setw(10) << setprecision(5) << params->XL[I+IOF][8]
                << setw(10) << setprecision(5) << params->XL[I+IOF][9]
                << setw(10) << setprecision(5) << params->XL[I+IOF][10]
                << setw(10) << setprecision(5) << params->XL[I+IOF][11] << endl;
        }
        //  !cp jun 96 ... (CONVERT sof MULTIPLICITY)(also changed in OUTPTR)
        for (I=1; I <= N; ++I)
        {
            if(static_cast<int>(params->A[I+IOF][5]/10) != 0 && params->XL[I+IOF][5] == 0)
            {
                params->XL[I+IOF][5]=1e-6;
            }
            params->XL[I+IOF][5] = params->XL[I+IOF][5] * multip->MURT[I+IOF] / multip->XMLTP[K];
        }
        // params->PAR[K][21] introduced below. It is for the term cot**2 in the pv-5 FWHM !cp Aug 95
        // line 11-5, line 11-6, line 11-7, line 11-8 and line 11-9
        getline(file5,s);			// S  O_B (line 11-5)
        stringstream(s.substr(0,8)) >> params->PAR[K][1];
        stringstream(s.substr(8,8)) >> params->PAR[K][2];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][1];
        stringstream(s.substr(8,8)) >> params->APAR[K][2];

        getline(file5,s);			// FWHM (line 11-6)
        stringstream(s.substr(0,8)) >> params->PAR[K][3];
        stringstream(s.substr(8,8)) >> params->PAR[K][4];
        stringstream(s.substr(16,8)) >> params->PAR[K][5];
        stringstream(s.substr(24,8)) >> params->PAR[K][21];
        stringstream(s.substr(32,8)) >> params->PAR[K][20];
        stringstream(s.substr(40,8)) >> params->PAR[K][15];
        stringstream(s.substr(48,8)) >> params->PAR[K][16];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][3];
        stringstream(s.substr(8,8)) >> params->APAR[K][4];
        stringstream(s.substr(16,8)) >> params->APAR[K][5];
        stringstream(s.substr(24,8)) >> params->APAR[K][21];
        stringstream(s.substr(32,8)) >> params->APAR[K][20];
        stringstream(s.substr(40,8)) >> params->APAR[K][15];
        stringstream(s.substr(48,8)) >> params->APAR[K][16];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->PAR[K][6];
        stringstream(s.substr(8,8)) >> params->PAR[K][7];
        stringstream(s.substr(16,8)) >> params->PAR[K][8];
        stringstream(s.substr(24,8)) >> params->PAR[K][9];
        stringstream(s.substr(32,8)) >> params->PAR[K][10];
        stringstream(s.substr(40,8)) >> params->PAR[K][11];

        getline(file5,s);			// Unit cell (line 11-7)
        stringstream(s.substr(0,8)) >> params->APAR[K][6];
        stringstream(s.substr(8,8)) >> params->APAR[K][7];
        stringstream(s.substr(16,8)) >> params->APAR[K][8];
        stringstream(s.substr(24,8)) >> params->APAR[K][9];
        stringstream(s.substr(32,8)) >> params->APAR[K][10];
        stringstream(s.substr(40,8)) >> params->APAR[K][11];

        getline(file5,s);			// G1 G2 P (line 11-8)
        stringstream(s.substr(0,8)) >> params->PAR[K][12];
        stringstream(s.substr(8,8)) >> params->PAR[K][13];
        stringstream(s.substr(16,8)) >> params->PAR[K][14];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][12];
        stringstream(s.substr(8,8)) >> params->APAR[K][13];
        stringstream(s.substr(16,8)) >> params->APAR[K][14];

        getline(file5,s);			// NA NB NC (line 11-91)
        stringstream(s.substr(0,8)) >> params->PAR[K][17];
        stringstream(s.substr(8,8)) >> params->PAR[K][18];
        stringstream(s.substr(16,8)) >> params->PAR[K][19];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][17];
        stringstream(s.substr(8,8)) >> params->APAR[K][18];
        stringstream(s.substr(16,8)) >> params->APAR[K][19];

        getline(file5,s);			// NA NB NC HS (line 11-93)
        stringstream(s.substr(0,8)) >> params->PAR[K][24];
        stringstream(s.substr(8,8)) >> params->PAR[K][25];
        stringstream(s.substr(16,8)) >> params->PAR[K][26];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][24];
        stringstream(s.substr(8,8)) >> params->APAR[K][25];
        stringstream(s.substr(16,8)) >> params->APAR[K][26];

        getline(file5,s);			// s-PVII (line 11-95);
        stringstream(s.substr(0,8)) >> params->PAR[K][27];

        getline(file5,s);
        stringstream(s.substr(0,8)) >> params->APAR[K][27];

        // checking for zeros if TCH-PV is being used
        if(cntrls->NPROF == _TCHZ)
        {
            if(params->PAR[K][15] == 0)params->PAR[K][15]=1e-8;
            if(params->PAR[K][16] == 0)params->PAR[K][16]=1e-8;
            if(params->PAR[K][3] == params->PAR[K][4] && params->PAR[K][4] == params->PAR[K][5] && params->PAR[K][5] == params->PAR[K][20] && params->PAR[K][3] == 0) params->PAR[K][5]=1e-8;
        }
        // checking for zeros in PV #5
        if(cntrls->NPROF == _pseudoVoigt)
        {
            if(params->PAR[K][17] == 0)params->PAR[K][17]=1e-6;
        }
        //      if(int(params->APAR[K][20]/10) != 0 && params->PAR[K][20] == 0)params->PAR[K][20]=1e-9
        // !cp Aug 95 introducing params->PAR[K][21]=ct
        // CHECKING FOR NON-REFINABLE PARAMETERS
        //CCC                        FOR  CT  IN TCHZ AND SPVII FUNCTIONS
        if(cntrls->NPROF == _TCHZ || cntrls->NPROF == _SplitPearsonVII)
        {
            params->PAR[K][21]=0.0;
            if(params->APAR[K][21] != 0.0)
            {
                cout << "NON-REFINABLE PARAMETER TURNED ON (CT) WITH SPLIT PEARSON VII OR TCHZ PROFILE FUNCTION" << endl;
                file6 << "NON-REFINABLE PARAMETER TURNED ON (CT) WITH SPLIT PEARSON VII OR TCHZ PROFILE FUNCTION" << endl;
                DBWSException("");
            }
        }
        //CCC                             FOR  X,Y,Z IN NON-TCHZ FUNCTION
        if(cntrls->NPROF != _TCHZ)
        {
            if(params->PAR[K][20] != 0 || params->PAR[K][16] != 0 || params->APAR[K][15] != 0)
            {
                file6 << "     NON-REFINABLE PARAMETER RESET TO ZERO (Z,X,Y) FOR" << endl
                    << "     NON TCHZ PROFILE FUNCTION" << endl;
                params->PAR[K][20]=0.0;
                params->PAR[K][16]=0.0;
                params->PAR[K][15]=0.0;
            }
            if(params->APAR[K][20] != 0 || params->APAR[K][16] != 0 || params->APAR[K][15] != 0)
            {
                cout << "NON-REFINABLE PARAMETER TURNED ON (Z,X,Y) WITH NOT TCHZ PROFILE FUNCTION" << endl;
                file6 << "NON-REFINABLE PARAMETER TURNED ON (Z,X,Y) WITH NOT TCHZ PROFILE FUNCTION" << endl;
                DBWSException("");
            }
        }
        //CCCC                            FOR  RIET_ASYM,X,Y,Z,CT IN SPVII
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            for (KKS=1; KKS <= 3; ++KKS)
            {
                if(params->PAR[K][13+KKS] != 0.0)
                {
                    params->PAR[K][13+KKS]=0.0;
                    file6 << " RIET_ASYM X Y RESET TO ZERO FOR SPVII FUNCTION" << endl;
                }
                if(params->APAR[K][13+KKS] != 0.0)
                {
                    cout << "NON-REFINEABLE PARAMETER TURNED ON (X,Y,R/RCF_ASYM)" << endl
                        << ", WITH SPLIT PEARSON VII PROFILE FUNCTION" << endl;
                    file6 << "NON-REFINEABLE PARAMETER TURNED ON (X,Y,R/RCF_ASYM)" << endl
                        << ", WITH SPLIT PEARSON VII PROFILE FUNCTION" << endl;
                    DBWSException("");
                }
            }
            for (KKS=1; KKS <= 2; ++KKS)
            {
                if ( params->PAR[K][19+KKS] != 0.0 )
                {
                    params->PAR[K][19+KKS]=0.0;
                    file6 << "CT,Z  RESET TO ZERO FOR SPVII FUNCTION" << endl;
                }
                if ( params->APAR[K][19+KKS] != 0.0 )
                {
                    cout << "NON-REFINEABLE PARAMETER TURNED ON (CT,Z), WITH SPLIT PEARSON VII PROFILE FUNCTION" << endl;
                    file6 << "NON-REFINEABLE PARAMETER TURNED ON (CT,Z), WITH SPLIT PEARSON VII PROFILE FUNCTION" << endl;
                    DBWSException("");
                }
            }
        }
        //CCCCC             FOR HIGH SIDE PARAMETERS IN NON-PVII PROFILE FUNCTION
        if (cntrls->NPROF != _SplitPearsonVII)
        {
            for (KK=1; KK <= 4; ++KK)
            {
                if ( params->PAR[K][23+KK] != 0.0 )
                {
                    params->PAR[K][23+KK]=0.0;
                    file6 << "NA NB NC (HIGH SIDE) AND PEARSON ASYMMETRY RESET TO ZERO FOR A, NON-SPLIT PEARSON VII FUNCTION" << endl;
                }
                if(params->PAR[K][23+KK] != 0.0)
                {
                    cout << "NA NB NC (HIGH SIDE) AND PEARSON ASYMMETRY ONLY REFINABLE FOR A SPLIT PEARSON VII FUNCTION" << endl;
                    file6 << "NA NB NC (HIGH SIDE) AND PEARSON ASYMMETRY ONLY REFINABLE FOR A SPLIT PEARSON VII FUNCTION" << endl;
                }
            }
        }
        //CCCCC END OF CHECKING NON-REFINABLE PARAMETERS
        file6 << "OVERALL SCALE FACTOR=" << scientific << setw(12) << setprecision(6) << params->PAR[K][1] << fixed << endl
            << "OVERALL TEMP. FACTOR=" << setw(12) << setprecision(5) << params->PAR[K][2] << endl
            << "DIRECT CELL PARAMETERS="
            << setw(9) << setprecision(4) << params->PAR[K][6]
        << setw(9) << setprecision(4) << params->PAR[K][7]
        << setw(9) << setprecision(4) << params->PAR[K][8]
        << setw(9) << setprecision(4) << params->PAR[K][9]
        << setw(9) << setprecision(4) << params->PAR[K][10]
        << setw(9) << setprecision(4) << params->PAR[K][11] << endl
            << "PREFERRED ORIENTATION PARAMETERS="
            << setw(7) << setprecision(3) << params->PAR[K][12]
        << setw(7) << setprecision(3) << params->PAR[K][13]
        << "ASYMMETRY PARAMETER="
            << setw(8) << setprecision(4) << params->PAR[K][14] << endl;

        //-----CHECK FOR SPLIT PEARSON PROFILE
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            file6 << "LOW SIDE EXPONENT COEFFICIENTS="
                << setw(12) << setprecision(4) << params->PAR[K][17]
            << setw(12) << setprecision(4) << params->PAR[K][18]
            << setw(12) << setprecision(4) << params->PAR[K][19]
            << "HIGH SIDE EXPONENT COEFFICIENTS="
                << setw(12) << setprecision(4) << params->PAR[K][24]
            << setw(12) << setprecision(4) << params->PAR[K][25]
            << setw(12) << setprecision(4) << params->PAR[K][26]
            << "SPLIT PEARSON VII ASYMMETRY PARAMETER="
                << setw(8) << setprecision(4) << params->PAR[K][27] << endl;
        }
        else
        {
            //-----if NOT THE SPLIT PEARSON VII PROFILE
            file6 << "MIXING PARAMETERS = "
                << scientific
                << setw(10) << setprecision(3) << params->PAR[K][17] << " "
                << setw(10) << setprecision(3) << params->PAR[K][18] << " "
                << setw(10) << setprecision(3) << params->PAR[K][19] << fixed << endl;
        }
        file6 << "FWHM PARAMETERS (U,V,W,CT,Z,X,Y)="
            << setw(9) << setprecision(4) << params->PAR[K][3]
        << setw(9) << setprecision(4) << params->PAR[K][4]
        << setw(9) << setprecision(4) << params->PAR[K][5]
        << setw(9) << setprecision(4) << params->PAR[K][21]
        << setw(9) << setprecision(4) << params->PAR[K][20]
        << setw(9) << setprecision(4) << params->PAR[K][15]
        << setw(9) << setprecision(4) << params->PAR[K][16] << endl;
        cellx->A=params->PAR[K][6];
        cellx->B=params->PAR[K][7];
        cellx->C=params->PAR[K][8];
        cellx->ALPHA=params->PAR[K][9];
        cellx->BETA=params->PAR[K][10];
        cellx->GAMMA=params->PAR[K][11];
        CELL2(K,LAMDAM);
        // ************************************** !cp ap 97 (from It code)
        if(cntrls->FONDO == 1 || cntrls->FONDO == 2)
        {
            bkgscale->SCABKG[K] = volume->GCOM[K] * params->PAR[K][1];
        }
        // ************************************************************
        file6 << "CELL VOLUME PHASE(" << setw(2) << K << " ) = " << setw(12) << setprecision(4) << volume->VOLI[K] << endl;
        for (I=1; I <= 6; ++I) dc->SAVE[K][I]=params->PAR[K][I+5];

        // change to reci
        for (I=1; I <= 3; ++I) params->PAR[K][I+5]=cellx->AL[I][I];			// a,b and c cell parameters
        params->PAR[K][9]=cellx->AL[2][3];			// alpha cell
        params->PAR[K][10]=cellx->AL[1][3];			// beta cell
        params->PAR[K][11]=cellx->AL[1][2];			// gamma cell
        file6 << "***Coding of variables***" << endl
            << "ATOM                        X         Y         Z         B         So" << endl
            << "                          B11       B22       B33       B12       B13       B23" << endl;
        //-----PRINT CODEWORDS FOR ATOMIC PARAMETERS
        for (I=1; I <= N; ++I)
        {
            file6 << setw(4) << parac->ATEXT[I+IOF]
            << "                 "
                << setw(10) << setprecision(2) << params->A[I+IOF][1]
            << setw(10) << setprecision(2) << params->A[I+IOF][2]
            << setw(10) << setprecision(2) << params->A[I+IOF][3]
            << setw(10) << setprecision(2) << params->A[I+IOF][4]
            << setw(10) << setprecision(2) << params->A[I+IOF][5] << endl
                << "                     "
                << setw(10) << setprecision(2) << params->A[I+IOF][6]
            << setw(10) << setprecision(2) << params->A[I+IOF][7]
            << setw(10) << setprecision(2) << params->A[I+IOF][8]
            << setw(10) << setprecision(2) << params->A[I+IOF][9]
            << setw(10) << setprecision(2) << params->A[I+IOF][10]
            << setw(10) << setprecision(2) << params->A[I+IOF][11] << endl;
        }
        for (I=1; I <= N; ++I)
        {
            for (J=1; J <= 11; ++J)
            {
                X=params->A[I+IOF][J];
                IYY=static_cast<int>(abs(X)/10.0);
                if(IYY > MSZ) goto L99996;
                params->LP[I+IOF][J]=IYY;
                params->A[I+IOF][J]=(abs(X)-10.*static_cast<double>(IYY))*sign(X);
            }
        }
        //-----PRINT CODEWORDS FOR PROFILE PARAMETERS
        file6 << "OVERALL SCALE FACTOR=" << setw(8) << setprecision(2) << params->APAR[K][1] << endl
            << "OVERALL TEMP. FACTOR=" << setw(8) << setprecision(2) << params->APAR[K][2] << endl
            << "DIRECT CELL PARAMETERS="
            << setw(8) << setprecision(2) << params->APAR[K][6]
        << setw(8) << setprecision(2) << params->APAR[K][7]
        << setw(8) << setprecision(2) << params->APAR[K][8]
        << setw(8) << setprecision(2) << params->APAR[K][9]
        << setw(8) << setprecision(2) << params->APAR[K][10]
        << setw(8) << setprecision(2) << params->APAR[K][11] << endl
            << "PREFERRED ORIENTATION PARAMETERS="
            << setw(8) << setprecision(2) << params->APAR[K][12]
        << setw(8) << setprecision(2) << params->APAR[K][13] << endl
            << "ASYMMETRY PARAMETER="
            << setw(8) << setprecision(4) << params->APAR[K][14] << endl;

        // !cp ap 97 (from It code)
        if(cntrls->FONDO == 1 && (params->PAR[K][2] != 0.0 || params->APAR[K][2] != 0.0)) goto L88888;
        if(cntrls->FONDO == 2 && params->PAR[K][2] == 0.0 && params->APAR[K][2] == 0.0) goto L88889;
        //-----CHECK FOR SPLIT PEARSON PROFILE
        if (cntrls->NPROF == _SplitPearsonVII)
        {
            file6 << "LOW SIDE EXPONENT COEFFICIENTS="
                << setw(8) << setprecision(2) << params->APAR[K][17]
            << setw(8) << setprecision(2) << params->APAR[K][18]
            << setw(8) << setprecision(2) << params->APAR[K][19] << endl
                << "HIGH SIDE EXPONENT COEFFICIENTS="
                << setw(8) << setprecision(2) << params->APAR[K][24]
            << setw(8) << setprecision(2) << params->APAR[K][25]
            << setw(8) << setprecision(2) << params->APAR[K][26] << endl
                << "SPLIT PEARSON VII ASSYMETRY PARAMETER="
                << setw(8) << setprecision(2) << params->APAR[K][27] << endl;
        }
        else
        {
            //-----if NOT THE SPLIT PEARSON VII PROFILE
            file6
                << "MIXING PARAMETERS = "
                << setw(10) << setprecision(3) << params->APAR[K][17]
                << setw(10) << setprecision(3) << params->APAR[K][18]
                << setw(10) << setprecision(3) << params->APAR[K][19] << endl;
        }
        file6
            << "FWHM PARAMETERS (U,V,W,CT,Z,X,Y)="
            << setw(8) << setprecision(2) << params->APAR[K][3]
            << setw(8) << setprecision(2) << params->APAR[K][4]
            << setw(8) << setprecision(2) << params->APAR[K][5]
            << setw(8) << setprecision(2) << params->APAR[K][21]
            << setw(8) << setprecision(2) << params->APAR[K][20]
            << setw(8) << setprecision(2) << params->APAR[K][15]
            << setw(8) << setprecision(2) << params->APAR[K][16] << endl;
        for (I=1; I <= 27; ++I)
        {
            X=params->APAR[K][I];
            IYY=static_cast<int>(abs(X)/10.0);
            params->LPAR[K][I]=IYY;
            params->APAR[K][I]=(abs(X)-10.*static_cast<double>(IYY))*sign(X);
        }
        //L151:
        LOOKUP(K,jnk->NATOM[K],jnk->NSCAT,IXRAY,cntrls->JOBTYP);
        if(cntrls->FONDO == 1 || cntrls->FONDO == 2) FINDC(K,jnk->NSCAT);
        g1->U=params->PAR[K][3];
        g1->V=params->PAR[K][4];
        g1->W=params->PAR[K][5];
        g1->ZZZ = params->PAR[K][20];
        g1->UC = params->PAR[K][21];
        if (cntrls->NPROF == _TCHZ)
        {
            g1->ULOR=params->PAR[K][15];
            g1->VLOR=params->PAR[K][16];
        }
        REFGEN(K,params->GLB[1],params->GLB[10],params->GLB[11],jnk->PREF,params->PAR[K][12]);
        simoper->ISIMOP=0;
        RTMT(&cntrls->IPL1,&K);
        ICY=1;
        ICZ=0;
        for (IIPHAS=1; IIPHAS <= K; ++IIPHAS) ICZ = ICZ + refls->ICR[IIPHAS];
        if(K >= 2) ICY=1+ICZ-refls->ICR[K];
        if(cntrls->LST1 != 1)goto L479;
        IXDEL=0;
        for (IXX=ICY; IXX <= ICZ; ++IXX)
        {
            IX=IXX-IXDEL;
            IRL=(refls->IREFS[IXX] % 256)-128;
            IRK=((refls->IREFS[IXX]/256) % 256)-128;
            IRH=((refls->IREFS[IXX]/(256*256)) % 256)-128;
            IRC=(refls->IREFS[IXX]/(256*256*256)) % 8;
            MLTT = static_cast<int>(refls->FMGNTD[IXX]);
            if (cntrls->NPROF == _TCHZ)
            {
                if((IX-1 % 60) == 0) file6 << "NO.  CODE    H   K   L  MULT   HW     POSN      FACTOR       HWL       HWG     ETA" << endl;
                file6 << setw(4) << IX
                    << setw(4) << IRC << "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(6) << MLTT
                    << setw(8) << setprecision(3) << refls->REFS[IXX][1]
                << setw(8) << setprecision(3) << refls->REFS[IXX][2]
                << setw(10) << setprecision(6) << refls->REFS[IXX][3]
                << setw(8) << setprecision(3) << refls->HALFL[IXX]
                << setw(8) << setprecision(3) << refls->HALFG[IXX]
                << setw(8) << setprecision(3) << refls->GAM[IXX] << endl;
            }
            else if (cntrls->NPROF == _SplitPearsonVII)
            {
                refls->FWHM[IXX][1]=2.0*(refls->REFS[IXX][1])*params->PAR[K][27]/(1.0+params->PAR[K][27]);
                refls->FWHM[IXX][2]=2.0*(refls->REFS[IXX][1])/(1.0+params->PAR[K][27]);
                if((IX-1 % 60) == 0) file6 << "NO.  CODE    H   K   L  MULT      HWL    HWH     FWHM   POSN    FACTOR" << endl;
                file6 << setw(4) << IX
                    << setw(4) << IRC << "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(6) << MLTT
                    << setw(8) << setprecision(3) << refls->FWHM[IXX][1]
                << setw(8) << setprecision(3) << refls->FWHM[IXX][2]
                << setw(8) << setprecision(3) << refls->REFS[IXX][1]
                << setw(8) << setprecision(3) << refls->REFS[IXX][2]
                << setw(10) << setprecision(6) << refls->REFS[IXX][3] << endl;
            }
            else
            {
                if ( (IX-1 % 60) == 0 ) file6 << "NO.  CODE    H   K   L  MULT     HW     POSN    FACTOR" << endl;
                file6 << setw(4) << IX
                    << setw(4) << IRC << "   "
                    << setw(4) << IRH
                    << setw(4) << IRK
                    << setw(4) << IRL
                    << setw(6) << MLTT
                    << setw(8) << setprecision(3) << refls->REFS[IXX][1]
                << setw(8) << setprecision(3) << refls->REFS[IXX][2]
                << setw(10) << setprecision(6) << refls->REFS[IXX][3] << endl;
            }
        }
L479:
        for (IX=ICY; IX <= ICZ; ++IX) refls->REFS[IX][3]=refls->REFS[IX][3]*refls->FMGNTD[IX]; // double(MLTT(IX))
        //L81:;
    }
    // end of great loop on phases

    // FIND CODEWORDS FOR GLOBAL PARAMETERS: LGLB(I) & AGLB(I)
    for (I=1; I <= 20; ++I)
    {
        X=params->AGLB[I];
        IYY=static_cast<int>(abs(X)/10.0);
        params->LGLB[I]=IYY;
        params->AGLB[I]=(abs(X)-10.*static_cast<double>(IYY))*sign(X);
    }
    qpainit();
    if (cntrls->JOBTYP > 2) return;
    NATOMS = 0;
    for (K=1; K <= cntrls->NPHASE; ++K) NATOMS = NATOMS + jnk->NATOM[K];
    //     COUNT NO. OF USES OF SAME LOCATION IN NORMAL MATRIX
    for (I=1; I <= cntrls->MAXS; ++I)
    {
        LCOUNT = 0;
        for (J=1; J <= 20; ++J) if ( I == params->LGLB[J] ) LCOUNT = LCOUNT + 1;
        for (K=1; K <= cntrls->NPHASE; ++K)
        {
            for (J=1; J <= 27; ++J) if ( I == params->LPAR[K][J] )  LCOUNT = LCOUNT + 1;
        }
        for (K=1; K <= NATOMS; ++K)
        {
            for (J=1; J <= 11; ++J) if(I == params->LP[K][J]) LCOUNT = LCOUNT + 1;
        }
        if (LCOUNT >= 2) file6 << "******* CODEWORD " << setw(3) << I << " is used " << setw(3) << LCOUNT << " times **" << endl;
    }
    //     END COUNT NO. OF USES OF SAME LOCATION IN NORMAL MATRIX
    //     CHECK FOR HOLES IN THE NORMAL MATRIX
    ISTOP=0;
    for (I=1; I <= cntrls->MAXS; ++I)
    {
        for (J=1; J <= 20; ++J) if (I == params->LGLB[J]) goto L7200;
        //  HOLES IN PHASE PARAMETER
        for (K=1; K <= cntrls->NPHASE; ++K)
        {
            for (J=1; J <= 27; ++J) if(I == params->LPAR[K][J]) goto L7200;
        }
        // HOLES IN ATOMS PARAMETERS
        for (K=1; K <= NATOMS; ++K)
        {
            for (J=1; J <= 11; ++J) if(I == params->LP[K][J]) goto L7200;
        }
        file7 << "     ***** HOLE IN THE MATRIX ******. ELEMENT " << setw(3) << I << " IN THE NORMAL MATRIX IS MISSING" << endl;
        file6 << "     ***** HOLE IN THE MATRIX ******. ELEMENT " << setw(3) << I << " IN THE NORMAL MATRIX IS MISSING" << endl;
        cout << "     ***** HOLE IN THE MATRIX ******. ELEMENT " << setw(3) << I << " IN THE NORMAL MATRIX IS MISSING" << endl;
        ISTOP = 1;
L7200:;
    }
    if (ISTOP == 1) DBWSException("");
    return;
    //L99999:
    //	DBWSException("END OF FILE TAPE5");
    //L99998:
    //	DBWSException("END OF FILE TAPE4");
    //L99997:
    //	DBWSException("END OF FILE TAPE3");
L99996:
    DBWSException("MATRIX SIZE IS TOO SMALL");
L99995:
    DBWSException("MAXS > MSZ");
L88888:
    DBWSException("WITH FONDO=1 YOU MUST USE ISOTROPIC THERMAL FACTORS");
L88889:
    DBWSException("if FONDO=2 YOU MUST USE THE OVERALL THERMAL FACTOR");
L88088:
    if (file4.is_open()) file4.close();
    if (file5.is_open()) file5.close();
    if (file6.is_open()) file6.close();
    if (file7.is_open()) file7.close();
    if (file8o.is_open()) file8o.close(); //	close(8,status='DELETE');
    if (file8i.is_open()) file8i.close();
    DBWSException("");
}

//ccccc subroutine inpam: To read the amorphous data file !cp may 10 97
void DBWS::inpam(void)
{
    int I;
    double TH,SW1, ABC, TMV1, STEP1, THMIN1, THMAX1;
    string s,DATAID1;


    // unit 11 = amorphous file
    file11.open(file11name.data());

    //-----CONTROL if THERE IS THE AMORPHOUS FILE .
    //-----AND if IT IS ON THE SAME POINTS OF THE DATA FILE
    getline(file11,s);
    stringstream(s.substr(0,8)) >> THMIN1;
    stringstream(s.substr(8,8)) >> STEP1;
    stringstream(s.substr(16,8)) >> THMAX1;
    stringstream(s.substr(24,8)) >> TMV1;
    stringstream(s.substr(32,8)) >> SW1;
    DATAID1 = s.substr(40,16);
    file6 << "DATA AMORPHOUS " << DATAID1 << endl;
    if (THMIN1 != g1->THMIN)
    {
        file6 << "AMORPHOUS THMIN="
            << setw(8) << setprecision(2) << THMIN1
            << "IS DIFFERENT FROM THE DATA THMIN="
            << setw(8) << setprecision(2) << g1->THMIN << endl;
    }
    if (STEP1 != g1->STEP)
    {
        file6 << "AMORPHOUS STEP="
            << setw(8) << setprecision(2) << STEP1
            << "IS DIFFERENT FROM THE DATA STEP="
            << setw(8) << setprecision(2) << g1->STEP << endl;
    }
    if (THMAX1 != g1->THMAX)
    {
        file6 << "AMORPHOUS THMAX="
            << setw(8) << setprecision(2) << THMAX1
            << "IS DIFFERENT FROM THE DATA THMAX="
            << setw(8) << setprecision(2) << g1->THMAX << endl;
    }
    //      read the rest of the file in free format
    for (I=1; I <= datax->NPTS; ++I) file11 >> datax->AMORPHOUS[I];
    for (I = 1; I <= datax->NPTS; ++I)
    {
        TH=g1->THMIN+(I-1)*g1->STEP;
        //------AMORPHOUS CORRECTION FOR AIR SCATTERING
        //       CALL ARIA(TMV1,SW1,FI1,TH,SCA)
        //       AMORPHOUS(I)=0.0
        if (cntrls->IAS == 1)
        {
            ABSORP(TMV1,SW1,TH,&ABC);
            datax->AMORPHOUS[I] = datax->AMORPHOUS[I] / ABC;
        }
    }
    return;
    //L99999:
    //	file6 << "END OF FILE ENCOUNTERED AT START ON TAPE = 11 OF PHASE AMORPHUS" << endl;
    //	DBWSException("*** END OF FILE ENCOUNTERED FOR AMORPHOUS FILE ***");
}



int main(int argc, char *argv[])
{
    DBWS app;

    if (argc > 0)
    {
        // unit 4=data file
        app.file4name = string(argv[1]);
        app.file4.open(app.file4name.data());

        if (argc > 1)
        {
            // unit 5 Input control File
            app.file5name = string(argv[2]);
            app.file5.open(app.file5name.data());

            if (argc > 2)
            {
                // unit 6 = output file
                app.file6name = string(argv[3]);
                app.file6.open(app.file6name.data());

                // unit 7 = r indexes
                app.file7.open("unit7");

                // unit 8 = scratch
                app.file8name = "unit8";
                app.file8o.open(app.file8name.data(),ios::trunc);			//open (8,file='unit8',form='unformatted',status='unknown')

                app.run();



                if ( app.file4.is_open() ) app.file4.close();
                if ( app.file5.is_open() ) app.file5.close();
                if ( app.file6.is_open() ) app.file6.close();
                if ( app.file7.is_open() ) app.file7.close();
                if ( app.file8o.is_open() ) app.file8o.close();    //close(8,status='DELETE');
                if ( app.file8i.is_open() ) app.file8i.close();
                if ( app.file11.is_open() ) app.file11.close();
                if ( app.file20.is_open() ) app.file20.close();

            }
            else
            {
                cout << "falta o nome do arquivo OUT" << endl;
            }
        }
        else
        {
            cout << "falta o nome do arquivo ICF" << endl;
        }
    }
    else
    {
        cout << "Falta o nome do arquivo RIT" << endl;
    }








    return 0;
}
